如何在C#中解析对象的JSON数组
本文关键字:对象 JSON 数组 | 更新日期: 2023-09-27 18:01:00
{
"jsonstring": [
{
"id":"01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt"
},
{
"id":"07",
"language": "C++",
"edition": "second",
"author": "E.Balagurusamy"
}
]
}
得到这样的post请求如何解析它。
使用NewtonSoft.NET:
var obj = JsonConvert.DeserializeObject(json);
也许也可以为使用泛型创建一个相应的类:
public class ClassName {
public string id { get; set; }
public string language { get; set; }
public string edition { get; set; }
public string author { get; set; }
}
那么你可以做:
List<ClassName> list = JsonConvert.DeserializeObject<List<ClassName>>(json);
在.NET中,您可以使用JavaScriptSerializer。首先,您需要创建一个与json字符串具有相同结构的类。
示例:
{
"Id":"07",
"Language": "C++"
}
public class CSharpObject
{
int Id {get; set;}
string Language {get; set;}
}
string json = HttpUtility.HtmlDecode(jsonString);
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
CSharpObject csharpObject = (CSharpObject)json_serializer.Deserialize<CSharpObject>(json);