我想阅读嵌套的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"
}
}
}
你空 } 附加图像已定义把你的字典编辑为'Dictionary',并且你得到一个key的'object'。这就是'Dictionary '所做的。如果你想让它返回一个对象以外的东西,不要使用'object'作为你的值类型。 –
我使用的任何东西,它认为的office_address作为对象的计数是0 – Sagar