抛出了Newtonsoft.Json.JsonSerializationException - Xamarin

时间:2017-06-08 17:11:04

标签: c# json

我正在使用我的应用上的实时Feed。转换为Json并尝试在我的应用程序上显示之后会抛出异常。我已经查看了许多与此问题相关的答案,但大多数答案与我在下面的代码中的答案相同?我该如何解决这个问题?

活动

{
    string result = new HTTPDataHandler().GetHTTPData(@params[0]);
    return result;
}

protected override void OnPostExecute(string result){
    RssObject data = JsonConvert.DeserializeObject<RssObject>(result);
    mDialog.Dismiss();
    DataAdapter adapter = new DataAdapter(data, mainActivity);
    mainActivity.rv.SetAdapter(adapter);
    adapter.NotifyDataSetChanged();
}

模型

public class Feed
{
    public string url { get; set; }
    public string title { get; set; }
    public string link { get; set; }
    public string author { get; set; }
    public string description { get; set; }
    public string image { get; set; }
}

public class Item
{
    public string title { get; set; }
    public string pubDate { get; set; }
    public string link { get; set; }
    public string guid { get; set; }
    public string author { get; set; }
    public string thumbnail { get; set; }
    public string description { get; set; }
    public string content { get; set; }
    public List<object> enclosure { get; set; }
    public List<string> categories { get; set; }
}

public class RssObject
{
    public string status { get; set; }
    public Feed feed { get; set; }
    public List<Item> items { get; set; }
}

错误

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.Object]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'items[0].enclosure.link', line 1, position 1142.

JSON

"status": "ok"
"feed": {
"url": "http://rss.nytimes.com/services/xml/rss/nyt/Technology.xml"
"title": "NYT &gt; Technology"
"link": "https://www.nytimes.com/section/technology?partner=rss&amp;emc=rss"
"author": ""
"description": ""
"image": "https://static01.nyt.com/images/misc/NYT_logo_rss_250x40.png"
}
"items": [
{
"title": "Uber Fires Executive Over Handling of Rape Investigation in India"
"pubDate": "2017-06-08 00:55:39"
"link": "https://www.nytimes.com/2017/06/07/technology/uber-fires-executive.html?partner=rss&amp;emc=rss"
"guid": "https://www.nytimes.com/2017/06/07/technology/uber-fires-executive.html"
"author": "MIKE ISAAC"
"thumbnail": ""
"description": "The president of Uber’s Asia business was terminated after reporters questioned why he obtained medical records of a woman attacked by her Uber driver."
"content": "The president of Uber’s Asia business was terminated after reporters questioned why he obtained medical records of a woman attacked by her Uber driver."
"enclosure": {
"link": "https://static01.nyt.com/images/2017/06/08/business/08UBER1-sub/08UBER1-sub-moth.jpg"
}
"categories": [
"0": "Uber Technologies Inc"
"1": "Sex Crimes"
"2": "Ethics and Official Misconduct"
"3": "Alexander, Eric"
]
}

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您正在尝试将RSS源转换为json。据我所知,在XML中,enclosure标签有三个必需属性“url”,“length”和“type”。所以第一个问题是外壳格式不正确。

假设json是你想要的那样。在json中,enclosure是一个对象,请注意{}而不是数组[]。但在C#中,它声明为List<object>,它通常表示json中的数组。因此,您尝试将object反序列化为List<object>

有几种方法可以解决这个问题。其一,如果您知道机箱的架构,则创建一个新类来表示架构,并将其用作类型(或List,如果它应该是一个数组)而不是List<object>。二,如果没有必要的外壳,只需删除C#类中的属性,因此在反序列化时可以忽略它。如果您需要机箱,但不知道架构可能是什么,请告诉我们,我们将花费更多周期为您找到解决方案。