我想读取嵌套的JSON并将数据存储在数据库中。我将JSON对象作为" application / json"从post方法到我的WCF服务。
为此,我将JSON转换为Dictionary。但只获得第一级值。获取第二级JSON的对象。
以下是我用来将JSON转换为Dictionary的代码:
[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);
}
}
}
以下是发布到WCF的JSON:
在此,获取对象而不是&#34; office_address&#34;,&#34; financial_details&#34;,&#34; residence_address&#34;和&#34; personal_details&#34;。
{
"business_name": "Test Business",
"fk_loan_id": "kh-aaaaa3232",
"proprietor_details": {
"office_address": {
"email_id": "aaa.aaa@aaa.com",
"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": "aaa.-aa@aa.com",
"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 :(得分: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”:“测试业务”, “fk_loan_id”:“kh-aaaaa3232”, “proprietor_details”:null, “date_of_incorporation”:“01/03/2010”, “financial_details”:{ “TAN”:“ghdg45341f”, “TIN”:“dc5345fg6g”, “增值税”:“dgdgd544t4”, “PAN”:“AAAAA7614A” }, “residence_address”:null, “personal_details”:null } 附加图像为小提琴手