解析嵌套(我认为)JSON

时间:2014-07-02 10:14:02

标签: c# json json.net

我一直试图解析这个JSON字符串。我使用JSON.NET和JSON片段,我的类和基本函数调用如下:

{"status":"ok","apirate":"0","people":{
"Mike":{"id":"Mike","rating":"0.80","questions":"100"},
"Donald":{"id":"Donald","rating":"0.7","questions":"9"},
"Tony":{"id":"Tony","rating":"0.22","questions":"2"},
"Penelope":{"id":"Penelope","rating":"0.006","questions":"6"},
"Sarah":{"id":"Sarah","rating":"0.79","questions":"20"},
"Thomas":{"id":"Thomas","rating":"0.12","questions":"25"},
"Gail":{"id":"Gail","rating":"0.44","questions":"35"}}}

我用作存储对象的类:

     public class Folks
    {
        public Folks()
        {
        }
        public String status;
        public String message; //optional
        public int apirate;
        public PeopleDetails[] people;
    }

      public class PeopleDetails 
    {
        public PeopleDetails ()
        {
        }
        public String id;
        public double rating;
        public int questions;
    }

最后,我在代码中做了什么:

    Folks test = new Folks();
    test = JsonConvert.DeserializeObject<Folks>(myRequest.GetResponse());

状态和API速率正常,消息不存在,因为没有错误,我的PeopleDetails数组正在异常。 (编辑:抛出一个JsonSerializationException,因为该类型需要一个JSON数组才能正确反序列化。)我已经尝试将另一个类/对象放在我粘贴的两个之间,并且我尝试过不同的集合,并且等等。

所以...因为这是我第一次使用这个(聪明,第一次选择复杂的东西),任何人都能指出我的解决方案吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

嗯,首先,你的给定JSON不正确,佩内洛普记录中缺少{

所以正确的JSON将是

{"status":"ok","apirate":"0","people":{
"Mike":{"id":"Mike","rating":"0.80","questions":"100"},
"Donald":{"id":"Donald","rating":"0.7","questions":"9"},
"Tony":{"id":"Tony","rating":"0.22","questions":"2"},
"Penelope":{"id":"Penelope","rating":"0.006","questions":"6"},
"Sarah":{"id":"Sarah","rating":"0.79","questions":"20"},
"Thomas":{"id":"Thomas","rating":"0.12","questions":"25"},
"Gail":{"id":"Gail","rating":"0.44","questions":"35"}}}

然后,如果你看一下结构,你可能不是那个人不是一个列表而是一个字典,名字是关键。

所以,这是一个有效的测试

    [TestMethod]
    public void Test()
    {
        var json = "{\"status\":\"ok\",\"apirate\":\"0\",\"people\":{\n\"Mike\":{\"id\":\"Mike\",\"rating\":\"0.80\",\"questions\":\"100\"},\n\"Donald\":{\"id\":\"Donald\",\"rating\":\"0.7\",\"questions\":\"9\"},\n\"Tony\":{\"id\":\"Tony\",\"rating\":\"0.22\",\"questions\":\"2\"},\n\"Penelope\":{\"id\":\"Penelope\",\"rating\":\"0.006\",\"questions\":\"6\"},\n\"Sarah\":{\"id\":\"Sarah\",\"rating\":\"0.79\",\"questions\":\"20\"},\n\"Thomas\":{\"id\":\"Thomas\",\"rating\":\"0.12\",\"questions\":\"25\"},\n\"Gail\":{\"id\":\"Gail\",\"rating\":\"0.44\",\"questions\":\"35\"}}}";
        var folks = JsonConvert.DeserializeObject<Folks>(json);

        Assert.AreEqual("ok", folks.Status);
    }

    public class Folks
    {
        public Folks()
        {
            this.People = new Dictionary<string, PeopleDetails>();
        }

        [JsonProperty("status")]
        public string Status { get; set; }

        [JsonProperty("message")]
        public string Message { get; set; }

        [JsonProperty("apirate")]
        public int Apirate { get; set; }

        [JsonProperty("people")]
        public Dictionary<string, PeopleDetails> People { get; set; }
    }

    public class PeopleDetails
    {
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("rating")]
        public decimal Rating { get; set; }

        [JsonProperty("questions")]
        public int Questions { get; set; }
    }