c# - 使用 Json.NET 的 Json 反序列化中的 JsonSerializationException

标签 c# windows-phone-7 json.net deserialization

我有一个这样的 Json :

[{"id":"54718","title":"Khaleda to visit China","corres":"Special Correspondent","details":"DHAKA: On a 7-day visit, opposition BNP Chairperson Khaleda Zia will leave Dhaka for China on October 14.","photo":"2012October\/SM\/Khaleda-new-sm20121003132805.jpg"}] 

为了解析这个 Json,到目前为止我已经完成了:

public class Attributes
{
    [JsonProperty("id")]
        public string ID{ get; set; }
        [JsonProperty("title")]
        public string TITLE { get; set; }
        [JsonProperty("corres")]
        public string CORRES { get; set; }
        [JsonProperty("details")]
        public string  DETAIL { get; set; }
        [JsonProperty("photo")]
        public string LINK { get; set; }
}

public class DataJsonAttributeContainer
{
    public List<Attributes> NewsList{ get; set; }
    //public Attributes attributes { get; set; }
}

public static T DeserializeFromJson<T>(string json)
{   //I'm getting the error here     
    T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
    return deserializedProduct;
}

& 在我的代码中:

 void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        //parse data
            var container = DeserializeFromJson<DataJsonAttributeContainer>(e.Result);

            //load into list
            for (i = 0; i < container.NewsList.Count ; i++)
            {
                newData[i] = new data();
                newData[i].id = container.NewsList[i].ID;
                newData[i].title = container.NewsList[i].TITLE;
                newData[i].price = container.NewsList[i].CORRES;
                newData[i].image = container.NewsList[i].DETAIL;
                newData[i].link = container.NewsList[i].LINK;
            }

问题是: container 正在从我可以在调试器中看到的 Web 服务器获取 json,但它在反序列化时遇到异常。有人可以帮忙吗? 我得到的异常:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'BanglaNewsPivot.MainPage+DataJsonAttributeContainer' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.Path '', line 1, position 1.

最佳答案

您的 json 是一个数组(不是包含数组的单个对象)。如下所示调用您的 DeserializeFromJson

var attrs = DeserializeFromJson<List<Attributes>>(e.Result);

足够了。

--编辑--

foreach (var attr in attrs)
{
     Console.WriteLine("{0} {1}", attr.ID, attr.TITLE);
}

关于c# - 使用 Json.NET 的 Json 反序列化中的 JsonSerializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12721013/

相关文章:

c# - 如何在 C# 中将拆分字符串(字符串数组)转换为 double 字符串?

c# - 并行查询 Azure 存储

c# - 如何在 Windows Phone 7 中创建自定义闹钟

c# - JToken.WriteToAsync 不会写入 JsonWriter

c# - 无法加载文件或程序集 'Newtonsoft.Json' 或其依赖项之一

c# - 将富文本框文本长度限制为明显合适的长度?

windows-phone-7 - 如何在Windows Phone 7.5中关闭后台音频代理?

windows-phone-7 - 构造函数与 onNavigateTo

f# - F# 的 Newtonsoft.Json DefaultSettings 问题

c# - c#中的单例问题