数组:
{
"field':["field1":"value1","field2":"value2"],
["field1":"value1","field2":"value2"]
}
如何在C#中解析上述JSON响应
您提供的json
字符串不符合json
格式,应该是以下json
数组:
{"field":[
{"field1":"value1","field2":"value2"},
{"field1":"value1","field2":"value2"}
]
}
json.net
进行转换:var obj = JsonConvert.DeserializeObject(json);
public class YourObject
{
public string Field1 { get; set; }
public string Field2 { get; set; }
}
public class YourClass
{
public YourObject[] Field { get; set; }
}
var yourClass = JsonConvert.DeserializeObject<YourClass>(json);
值得一提的是,可以看一下JavaScriptSerializer类以及其中的反序列化方法。
JavaScriptSerializer jss= new JavaScriptSerializer();
User user = jss.Deserialize<User>(jsonResponse);