2016-04-20 159 views
0

我想阅读嵌套的JSON并将数据存储在数据库中。我将JSON对象作为“application/json”从post方法传递给我的WCF服务。阅读嵌套的JSON对象

为此,我将JSON转换为字典。但只获得第一级值。获取二级JSON的对象。

下面是我使用的隐蔽JSON到字典中的代码:

[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); 
     } 
    } 
} 

下面是JSON发布到WCF:

在此,越来越对象,而不是值“office_address” “financial_details”,“residence_address”和“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

你空 } 附加图像已定义把你的字典编辑为'Dictionary ',并且你得到一个key的'object'。这就是'Dictionary '所做的。如果你想让它返回一个对象以外的东西,不要使用'object'作为你的值类型。 –

+0

我使用的任何东西,它认为的office_address作为对象的计数是0 – Sagar

回答

0

使用此 - var innerProperties = yourObject.proprietor_details;

这会给你里面proprietor_details属性。

我在这里创造一个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; } 
    } 

使其他类也......

这里是电话后我是JSON数据使用 { “business_name”:“Test Business”, “fk_loan_id”:“kh-aaaaa3232”, “propriet or_details“:空, ”date_of_incorporation“: ”01/03/2010“, ”financial_details“:{ ”TAN“: ”ghdg45341f“, ”天“: ”dc5345fg6g“, ”增值税“: ”dgdgd544t4“ , “PAN”: “AAAAA7614A” }, “residence_address”:空, “personal_details”:为小提琴手呼叫 enter image description here

连接数据树形象 [][1]

+0

嗨,我使用下面的方法来读取创建的字典:public Main ApplyNewLoan(ApplyNewLoan applyloan) { var innerProperties = applyloan.proprietor_details; return db.ApplyLoan(applyloan); } 但是,我可以阅读upto proprietor_details,作为jSonDictionary。我还需要阅读“office_address”。但它正在将它作为一个对象来阅读。 – Sagar

+0

然后,你应该尝试yourObject.proprietor_details.office_address – Rajdeep

+0

不工作,找不到“office_address” – Sagar