如何反序列化Fixture JSON?(How to deserialize a Fixture JSON?)

编程入门 行业动态 更新时间:2024-10-25 21:31:16
如何反序列化Fixture JSON?(How to deserialize a Fixture JSON?)

我正在尝试反序化这个json 。 首先,我使用了json2charp来创建类。 但是,我不确定生成的类在这种情况下是否正确。 但是我做了一个Http请求:

public string Request(string requestUrl) { HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest; request.Method = "GET"; request.ContentType = "application/json"; string responseText; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) using (var responseStream = new StreamReader(response.GetResponseStream())) { responseText = responseStream.ReadToEnd(); } return responseText; }

使用此函数,我从JSON传递反序列化的链接,然后我将返回带有响应的字符串。 此时我将响应保存到变量中:

string responseText = parser.Request(requestUrl);

然后创建一个对象,其中包含先前使用工具json2csharp生成的RootObject中定义的参数列表。 问题是foreach不接受该项,说没有GetEnumerator的定义。

var obj = JsonConvert.DeserializeObject<Fixtures.RootObject>(responseText); foreach (var element in obj) //this insert the value into a Datagrid. { MainWindow.AppWindow.Fixtures_Table.Items.Add(new Fixtures.Fixture { date = element.date; etc... }); }

json结构:

fixture:": { "_links": { "self": { "href": "http://api.football-data.org/alpha/fixtures/133566" }, "soccerseason": { "href": "http://api.football-data.org/alpha/soccerseasons/347" }, "homeTeam": { "href": "http://api.football-data.org/alpha/teams/556" }, "awayTeam": { "href": "http://api.football-data.org/alpha/teams/514" } }, "date": "2013-10-04T22:00:00Z", "matchday": 10, "homeTeamName": "Nimes Olympique", "awayTeamName": "SM Caen", "result": { "goalsHomeTeam": 2, "goalsAwayTeam": 1 } }, "head2head": { "count": 10, "timeFrameStart": "2014-05-12T22:00:00Z", "timeFrameEnd": "2000-09-29T22:00:00Z", "homeTeamWins": 1, "awayTeamWins": 5, "draws": 4, "lastHomeWinHomeTeam": { }, "lastWinHomeTeam": { }, "lastAwayWinAwayTeam": { }, "lastWinAwayTeam": { }, "fixtures": [ ] } }

所以我想知道我做错了什么,以及生成的类是否好。 如果其他人能够更加智能和有效地反序列化JSON,我很高兴看到它!

I'm trying to deserialize this json. First of all, I've used json2charp for create the class. I'm not sure, however, that the generated class is correct in this case. However I made an Http request:

public string Request(string requestUrl) { HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest; request.Method = "GET"; request.ContentType = "application/json"; string responseText; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) using (var responseStream = new StreamReader(response.GetResponseStream())) { responseText = responseStream.ReadToEnd(); } return responseText; }

With this function, I pass the link of deserialize from JSON, and I will return a string with the response. At this point I save the response into a variable:

string responseText = parser.Request(requestUrl);

Then create an object containing the list of parameters defined in RootObject previously generated with the tool json2csharp. The problem is that the foreach will not accept the item, saying that there is no definition of GetEnumerator.

var obj = JsonConvert.DeserializeObject<Fixtures.RootObject>(responseText); foreach (var element in obj) //this insert the value into a Datagrid. { MainWindow.AppWindow.Fixtures_Table.Items.Add(new Fixtures.Fixture { date = element.date; etc... }); }

json structure:

fixture:": { "_links": { "self": { "href": "http://api.football-data.org/alpha/fixtures/133566" }, "soccerseason": { "href": "http://api.football-data.org/alpha/soccerseasons/347" }, "homeTeam": { "href": "http://api.football-data.org/alpha/teams/556" }, "awayTeam": { "href": "http://api.football-data.org/alpha/teams/514" } }, "date": "2013-10-04T22:00:00Z", "matchday": 10, "homeTeamName": "Nimes Olympique", "awayTeamName": "SM Caen", "result": { "goalsHomeTeam": 2, "goalsAwayTeam": 1 } }, "head2head": { "count": 10, "timeFrameStart": "2014-05-12T22:00:00Z", "timeFrameEnd": "2000-09-29T22:00:00Z", "homeTeamWins": 1, "awayTeamWins": 5, "draws": 4, "lastHomeWinHomeTeam": { }, "lastWinHomeTeam": { }, "lastAwayWinAwayTeam": { }, "lastWinAwayTeam": { }, "fixtures": [ ] } }

So I want to know if I'm doing something wrong and if the generated class is good or not. If someone else can deserialize the JSON in a more intelligent and effective, I am happy to read it!

最满意答案

没有枚举器,因为你的JSON只是对象而不是数组,所以除非你遍历键来获取值,否则没有什么可以真正迭代。 因此,我希望Fixtures.RootObject不是实现IEnumerable的东西。 如果生成的RootObject跟随你的json,我希望以下工作:

foreach (var element in obj.head2head.fixtures) { ... }

如果没有,我会检查json.net并将json反序列化为动态或匿名对象以使用上面的符号。

var obj = JsonConvert.DeserializeObject<dynamic>(responseText); foreach (var element in obj.head2head.fixtures) { ... }

There is no enumerator because your JSON is nothing but objects instead of an array, so there is nothing to really iterate over unless you iterate over the keys to get the values. Therefore, I would expect Fixtures.RootObject to not be something implementing IEnumerable. If the generated RootObject follows your json, I would expect the following to work:

foreach (var element in obj.head2head.fixtures) { ... }

If not, I'd check out json.net and deserialize the json into a dynamic or anonymous object to use the notation above.

var obj = JsonConvert.DeserializeObject<dynamic>(responseText); foreach (var element in obj.head2head.fixtures) { ... }

更多推荐

本文发布于:2023-08-04 13:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415924.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   Fixture   deserialize   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!