如何使用JSON.NET使用带有对象的数组反序列化JSON对象

时间:2016-07-06 16:24:41

标签: c# arrays json json.net json-deserialization

我有这个JSON文件,我想反序列化。

{
 "agencies":[
 {
  "id":44,
  "name":"National Aeronautics and Space Administration",  
  "countryCode":"USA",
  "abbrev":"NASA",
  "type":1,"infoURL":"http:\/\/www.nasa.gov",
  "wikiURL":"http:\/\/en.wikipedia.org\/wiki\/National_Aeronautics_and_Space_Administration",
  "infoURLs":[
  "http:\/\/www.nasa.gov"
  ]
 }
],
"total":0,
"count":1,
"offset":0
}

我当前有这个代码来获取JSON并尝试反序列化它。

        RestClient client = new RestClient("https://launchlibrary.net/1.2/agency");
        RestRequest request = new RestRequest("NASA",Method.GET);
        var results = client.Execute(request).Content;

        MessageBox.Show(JsonConvert.DeserializeObject(results).ToString());

        // Deserialize

        RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(results);

        MessageBox.Show(rootObject.agencies.Count.ToString());

这个

public class Agency
{
    public int id { get; set; }
    public string name { get; set; }
    public string countryCode { get; set; }
    public string abbrev { get; set; }
    public int type { get; set; }
    public string infoURL { get; set; }
    public string wikiURL { get; set; }
    public List<string> infoURLs { get; set; }
}

public class RootObject
{
    public List<Agency> agencies { get; set; }
    public int total { get; set; }
    public int count { get; set; }
    public int offset { get; set; }
}

我可以轻松访问对象内部的内容,例如totalcount,但不能访问数组agencies

我该怎么做?

我只对agencies数组中的数据感兴趣,而不是totalcountoffset

0 个答案:

没有答案