将Json字符串反序列化为多种对象类型

编程入门 行业动态 更新时间:2024-10-28 01:27:33
本文介绍了将Json字符串反序列化为多种对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个从Web服务获取的Json字符串;它有一个集合列表,每个集合代表一个对象,例如:

I have a Json String that I get from a web service; it has a list of collections, each collection represents an object, for example:

[ // Root List [ // First Collection : Team Object { "id": 1, "team_name": "Equipe Saidi", "is_active": true, "last_localisation_date": "2015-05-06T13:33:15+02:00" }, { "id": 3, "team_name": "Equipe Kamal", "is_active": true, "last_localisation_date": "2015-05-06T09:22:15+02:00" } ], [// Second Collection : user Object { "id": 1, "login": "khalil", "mobile_password": "####", "first_name": "Abdelali", "last_name": "KHALIL", "email": "KHALIL@gmail", "role": "DR", "is_active": true, "charge": false }, { "id": 2, "login": "ilhami", "mobile_password": "####", "first_name": "Abdellah", "last_name": "ILHAMI", "email": "ILHAMI@gmail", "role": "DR", "is_active": true, "charge": false } ] ]

我的实际代码(当然不起作用)

My actual code (not working of course ):

public async Task TeamsAndMobileUsers() { string data = ""; IList<User> MobileUsersList = new List<User>(); IList<Team> TeamsList = new List<Team>(); try { data = await GetResponse(PATH + TEAMS_USERS_URL); TeamsList = JsonConvert.DeserializeObject<List<Team>>(data); MobileUsersList = JsonConvert.DeserializeObject<List<User>>(data); // Inserting await SetAchievedActions(TeamsList); } catch (Exception e) { _errors.Add(e.Message); } }

我使用Json和C#.我找不到解决方案,我读过我应该使用JsonReader并将其SupportMultipleContent属性设置为true,但我不知道如何实现该解决方案.

I use Json and C#. I can't find a solution, I've read that I should use JsonReader and set its SupportMultipleContent property to true but I don't know how to implement that solution.

推荐答案

正如@YeldarKurmangaliyev已经说过的那样,您的json有两个不同的对象,我认为您可以执行以下操作:

As @YeldarKurmangaliyev already said, your json has two different objects, I think you can do something like this:

var j = JArray.Parse(data); TeamsList = JsonConvert.DeserializeObject<List<Team>>(j[1].ToString()); MobileUsersList = JsonConvert.DeserializeObject<List<User>>(j[2].ToString());

更多推荐

将Json字符串反序列化为多种对象类型

本文发布于:2023-11-17 07:45:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609347.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   序列   多种   对象   类型

发布评论

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

>www.elefans.com

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