[英]Newtonsoft.Json.JsonSerializationException has been thrown - Xamarin
I am working with live feeds on my app. 我正在使用我的应用上的实时Feed。 After converting to Json and trying to display on my application is get this an exception thrown.
转换为Json并尝试在我的应用程序上显示之后会抛出异常。 I have looked at many answers relating to this issue but most answers are just the same as what i have in my code below?
我已经查看了许多与此问题相关的答案,但大多数答案与我在下面的代码中的答案相同? How can i resolve this problem?
我该如何解决这个问题?
Activity 活动
{
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();
}
Model 模型
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; }
}
error 错误
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 JSON
"status": "ok"
"feed": {
"url": "http://rss.nytimes.com/services/xml/rss/nyt/Technology.xml"
"title": "NYT > Technology"
"link": "https://www.nytimes.com/section/technology?partner=rss&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&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"
]
}
If I understood correctly, you're trying work on an RSS feed converted into json. 如果我理解正确,你正在尝试将RSS源转换为json。 As far as I remember, in XML the enclosure tag has three required attributes "url", "length", and "type".
据我所知,在XML中,enclosure标签有三个必需属性“url”,“length”和“type”。 So first issue is the enclosure is ill-formatted.
所以第一个问题是机箱格式不正确。
Assuming that json is how you want it to be. 假设json是你想要的那样。 In json, enclosure is an object, notice
{}
not an array []
. 在json中,enclosure是一个对象,注意
{}
不是数组[]
。 But in C# its declared as List<object>
which usually represents an array in json. 但在C#中,它声明为
List<object>
,它通常表示json中的数组。 So you're trying to deserialize an object
into an List<object>
. 因此,您尝试将
object
反序列化为List<object>
。
There are couple of ways you can resolve this. 有几种方法可以解决这个问题。 One, if you know the schema of enclosure, create a new class to represent the schema and use it as type (or List if its supposed to be an array) instead of
List<object>
. 其一,如果您知道机箱的架构,则创建一个新类来表示架构,并将其用作类型(或List,如果它应该是一个数组)而不是
List<object>
。 Two, if enclosure is not necessary, just remove the property in C# class, so it can be ignored on deserialization. 二,如果没有必要的外壳,只需删除C#类中的属性,因此在反序列化时可以忽略它。 If you need enclosure, but don't know what the schema could be, let us know, we'll spend few more cycles to find a solution for you.
如果您需要机箱,但不知道架构可能是什么,请告诉我们,我们将花费更多周期来为您找到解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.