如何在C#中解析JSON对象数组

时间:2016-08-04 11:37:11

标签: c# json

{
   "jsonstring": [

  {
     "id":"01",
     "language": "Java",
     "edition": "third",
     "author": "Herbert Schildt"
  },

  {
     "id":"07",
     "language": "C++",
     "edition": "second",
     "author": "E.Balagurusamy"
  }


   ]
}

获取这样的帖子请求如何解析它。

2 个答案:

答案 0 :(得分:6)

使用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);

答案 1 :(得分:1)

在.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);