【问题标题】:JSON parsing in windows phone returns an exceptionwindows phone 中的 JSON 解析返回异常
【发布时间】:2013-05-11 00:55:50
【问题描述】:

我正在为 Windows Phone 中的 Json 解析做一个示例。我正在使用 Json.Net (json.codeplex.com/releases/view/78509) 库来解析响应字符串。但是在解析时出现异常。例外是“无法将 JSON 数组反序列化为“System.String”类型。我在下面发布我的代码。

我收到的 JSON 响应

[{"runtime": ["194 min"], "rating": 7.6, "genres": ["Drama", "Romance"], "rated": "PG_13", "language": ["英语”、“法语”、“德语”、“瑞典语”、“意大利语”、“俄语”]、“标题”:“泰坦尼克号”、“filming_locations”:“美国加利福尼亚州圣克拉丽塔”、“海报”:“ http://ia.media-imdb.com/images/M/MV5BMjExNzM0NDM0N15BMl5BanBnXkFtZTcwMzkxOTUwNw@@._V1._SY317_CR0,0,214,317_.jpg", "imdb_url": "http://www.imdb.com/title/tt0120338/", "writers": ["James Cameron"], "imdb_id": "tt0120338", "directors": ["James Cameron"], "rating_count": 426376 、“演员”:[“莱昂纳多·迪卡普里奥”、“凯特·温斯莱特”、“比利·赞恩”、“凯西·贝茨”、“弗朗西斯·费舍尔”、“格洛丽亚·斯图尔特”、“比尔·帕克斯顿”、“伯纳德·希尔”、“大卫·华纳” , "Victor Garber", "Jonathan Hyde", "Suzy Amis", "Lewis Abernathy", "Nicholas Cascone", "Anatoly M. Sagalevitch"], "plot_simple": "一个十七岁的贵族,期望成为由她的母亲嫁给了一个富有的索赔人,在豪华、命运多舛的泰坦尼克号上爱上了一位善良但贫穷的艺术家。”,“年份”:1997,“国家”:[“美国”],“类型”: “M”,“release_date”:19980403,“also_known_as”:[“泰坦尼克号 3D”]}]

而解析代码是

private void ParseResult(string input)
{
   var root =  Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject1[]>(input);// here getting the exception "Cannot deserialize JSON array into type 'System.String"
}

对象类是

 public class RootObject1
{
    public string runtime { get; set; }
    public int rating { get; set; }
    public string rated { get; set; }
    public string title { get; set; }
    public string poster { get; set; }
    public string imdb_url { get; set; }
    public string writers { get; set; }
    public string imdb_id { get; set; }

}

谢谢。

【问题讨论】:

    标签: c# json windows-phone json.net


    【解决方案1】:

    好吧,您的对象与输入不对应。 “runtime”和“writers”字段是字符串数组,而 rating 不是整数值,所以正确的对象必须是这样的:

        public class RootObject1
        {
            public string[] runtime { get; set; }
            public float rating { get; set; }
            public string rated { get; set; }
            public string title { get; set; }
            public string poster { get; set; }
            public string imdb_url { get; set; }
            public string[] writers { get; set; }
            public string imdb_id { get; set; }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-05
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      相关资源
      最近更新 更多