加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Deserealization无法正常工作

发布时间:2020-12-15 22:16:17 所属栏目:百科 来源:网络整理
导读:我已经创建了一个反序列化 this JSON的类 public class Self{ public string href { get; set; }}public class Team{ public string href { get; set; }}public class Links{ public Self _self { get; set; } public Team team { get; set; }}public class
我已经创建了一个反序列化 this JSON的类

public class Self
{
    public string href { get; set; }
}

public class Team
{
    public string href { get; set; }
}

public class Links
{
    public Self _self { get; set; }
    public Team team { get; set; }
}

public class Player
{
    public int id { get; set; }
    public string name { get; set; }
    public string position { get; set; }
    public int jerseyNumber { get; set; }
    public string dateOfBirth { get; set; }
    public string nationality { get; set; }
    public string contractUntil { get; set; }
    public string marketValue { get; set; }
}

public class RootObject
{
    public Links _links { get; set; }
    public int count { get; set; }
    public List<Player> players { get; set; }
}

public struct Player_Struct
{
    public string id;
    public string name;
    public string position;
    public int jerseyNumber;
    public string dateOfBirth;
    public string nationality;
    public string contractUntil;
    public string marketValue;
}

所以我创建了一个函数来创建一个HttpRequest和相关对象:

string requestUrl = teams.link_teams;
 string responseText = parser.Request(requestUrl);
 var obj = JsonConvert.DeserializeObject<Players.RootObject>(responseText);

现在的问题是编译器返回此异常:

Unhandled exception ‘Newtonsoft.Json.JsonSerializationException’ in Newtonsoft.Json.dll

Ulteriori informazioni: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type ‘SF_DebugProject.API.Players+Links’ 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 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 ‘_links’,line 1,position 11.

所以使用修复提示我试图修复错误将对象更改为:

List<Players.RootObject> obj = JsonConvert.DeserializeObject<List<Players.RootObject>>(responseText);

但是这样,当我做foreach时,我看不到rootobject的任何属性.我做错了什么?

解决方法

根据我的理解,您希望在包含播放器列表的RootObject中反序列化JSON字符串.

这些类应如下所示:

public class Rootobject
{
    public _Links _links { get; set; }
    public int count { get; set; }
    public Player[] players { get; set; }
}

public class _Links
{
    public _Self _self { get; set; }
    public Team team { get; set; }
}

public class _Self
{
    public string href { get; set; }
}

public class Team
{
    public string href { get; set; }
}

public class Player
{
    public int id { get; set; }
    public string name { get; set; }
    public string position { get; set; }
    public int jerseyNumber { get; set; }
    public string dateOfBirth { get; set; }
    public string nationality { get; set; }
    public string contractUntil { get; set; }
    public string marketValue { get; set; }
}

所以你应该做的事情如下:

var obj = JsonConvert.DeserializeObject<RootObject>(response);
foreach(var player in obj.players) 
{
     // some stuff
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读