2016-04-20 12 views
0

Ich möchte verschachtelten JSON lesen und die Daten in der Datenbank speichern. Ich bekomme JSON-Objekt als "Anwendung/Json" von Post-Methode zu meinem WCF-Service.Gelöschtes JSON-Objekt lesen

Dazu konvertiere ich die JSON zu Dictionary. Aber nur erste Level-Werte bekommen. Objekt für JSON der zweiten Ebene abrufen

Unten ist der Code, den ich JSON verdeckte bin mit Wörterbuch:

[Serializable] 
public class JsonDictionary : ISerializable 
{ 
    private Dictionary<string, object> m_entries; 

    public JsonDictionary() 
    { 
     m_entries = new Dictionary<string, object>(); 
    } 

    public IEnumerable<KeyValuePair<string, object>> Entries 
    { 
     get { return m_entries; } 
    } 

    protected JsonDictionary(SerializationInfo info, StreamingContext context) 
    { 
     m_entries = new Dictionary<string, object>(); 
     foreach (var entry in info) 
     { 
      m_entries.Add(entry.Name, entry.Value); 
     } 
    } 

    public void GetObjectData(SerializationInfo info, StreamingContext context) 
    { 
     foreach (var entry in m_entries) 
     { 
      info.AddValue(entry.Key, entry.Value); 
     } 
    } 
} 

Unten ist die JSON-Buchung auf der WCF: diese

In, Objekt bekommen statt Wert für "office_address", "financial_details", "residence_address" und "personal_details".

{ 
    "business_name": "Test Business", 
    "fk_loan_id": "kh-aaaaa3232", 
    "proprietor_details": { 
     "office_address": { 
      "email_id": "[email protected]", 
      "alternative_mobile_number": "00000", 
      "pincode": "00000", 
      "landline_number": "00000", 
      "city": "Tutne", 
      "flat_number": "000", 
      "street": "Test Street", 
      "locality": "tLocality", 
      "state": "SGDS", 
      "mobile_number": "0000000000" 
     }, 
     "date_of_incorporation": "01/03/2010", 
     "financial_details": { 
      "TAN": "ghdg45341f", 
      "TIN": "dc5345fg6g", 
      "VAT": "dgdgd544t4", 
      "PAN": "AAAAA7614A" 
     }, 
     "residence_address": { 
      "email_id": "[email protected]", 
      "alternative_mobile_number": "0000000", 
      "pincode": "00000", 
      "landline_number": "00000", 
      "city": "SFSS", 
      "flat_number": "055", 
      "street": "DASAW", 
      "locality": "Local", 
      "state": "AAAAA", 
      "mobile_number": "000000" 
     }, 
     "personal_details": { 
      "gender": "M", 
      "date_of_birth": "06/07/1969", 
      "last_name": "AAA", 
      "middle_name": "A", 
      "first_name": "AAAA" 
     } 
    } 
} 
+0

Sie habe definit Editiert Ihr Wörterbuch als 'Dictionary ' und Sie erhalten ein 'Objekt' für einen Schlüssel. Das macht ein 'Dictionary '. Wenn Sie möchten, dass etwas anderes als ein Objekt zurückgegeben wird, verwenden Sie nicht "object" als Werttyp. –

+0

Alles, was ich benutze, es betrachtet die office_address als Objekt, deren Zählung 0 – Sagar

Antwort

0

verwenden - var innerProperties = yourObject.proprietor_details;

Dieses gibt Ihnen die Eigenschaften im Inneren proprietor_details. auch

habe ich eine mvc web api

namespace TestWebApi.Controllers 
{ 
public class ValuesController : ApiController 
{ 
    // GET api/values 
    public IEnumerable<string> Get() 
    { 
     return new string[] { "value1", "value2" }; 
    } 

    // GET api/values/5 
    public string Get(int id) 
    { 
     return "value"; 
    } 

    // POST api/values 
    public void Post(ComplexClass data) 
    { 
     var xx = data.business_name; 
     var y = xx + data.date_of_incorporation; 
    } 

    // PUT api/values/5 
    public void Put(int id, [FromBody]string value) 
    { 
    } 

    // DELETE api/values/5 
    public void Delete(int id) 
    { 
    } 
} 

}

public class ComplexClass 
{ 
    public string business_name { get; set; } 
    public string fk_loan_id { get; set; } 
    public proprietor_details proprietor_details {get;set;} 
    public string date_of_incorporation { get; set; } 
    public financial_details financial_details { get; set; } 
    public residence_address residence_address {get;set;} 
    public personal_details personal_details {get;set;} 
} 

public class financial_details 
{ 
    public string TAN { get; set; } 
    public string TIN { get; set; } 
    public string VAT { get; set; } 
    public string PAN { get; set; } 
    } 

machen andere Klassen der Code hier ist ...

hier die JSON-Daten für die Zeit nach Anruf ich bin mit { "business_name": "Testgeschäft", "fk_loan_id": "kh-aaaaa3232", "propriet or_details ": null, "date_of_incorporation": "01.03.2010", "financial_details": { "TAN": "ghdg45341f", "TIN": "dc5345fg6g", "VAT": "dgdgd544t4" , "PAN": "AAAAA7614A" }, "residence_address": null, "personal_details": null } Befestigung für Fiedler Anruf enter image description here

Anbringen Datenbaum Bild [][1]

+0

Hallo, ich unten Methode verwenden erstellt Wörterbuch zu lesen: public Haupt ApplyNewLoan (ApplyNewLoan applyloan) { var innerProperties = applyloan.proprietor_details; Rückgabe db.ApplyLoan (Applyloan); } Aber mit diesem kann ich bis zu property_details lesen, als jSonDictionary. Ich muss auch "office_address" lesen. Aber es liest es als ein Objekt. – Sagar

+0

Dann sollten Sie versuchen, IhreObject.proprietor_details.office_address – Rajdeep

+0

nicht funktioniert, kann nicht finden "office_address" – Sagar

Verwandte Themen