gpt4 book ai didi

c# - JSON解析C#无名称变量

转载 作者:行者123 更新时间:2023-11-30 23:31:06 25 4
gpt4 key购买 nike

我正在尝试将 Json 字符串反序列化为一个对象,但我只得到 0 和 null。

这是我的代码:

string result = "[{\"page\":1,\"pages\":1,\"per_page\":\"50\",\"total\":1},[{\"id\":\"BEL\",\"iso2Code\":\"BE\",\"name\":\"Belgium\",\"region\":{ \"id\":\"ECS\",\"value\":\"Europe & Central Asia(all income levels)\"},\"adminregion\":{ \"id\":\"\",\"value\":\"\"},\"incomeLevel\":{ \"id\":\"OEC\",\"value\":\"High income: OECD\"},\"lendingType\":{ \"id\":\"LNX\",\"value\":\"Not classified\"},\"capitalCity\":\"Brussels\",\"longitude\":\"4.36761\",\"latitude\":\"50.8371\"}]]";

var serializer = new DataContractJsonSerializer(typeof(LandRootObject));

var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));

var data = (LandRootObject)serializer.ReadObject(ms);


public class LandRootObject
{
public int page { get; set; }
public int pages { get; set; }
public string per_page { get; set; }
public int total { get; set; }

[DataMember]
public List<Land> land { get; set; }
}

谢谢!

最佳答案

我已经测试了这个方法并且它正在工作。

您的实体类。 (我没有编写所有这些类的代码。它们是使用特殊粘贴生成的代码。)

public class LandRootObject
{
public int page { get; set; }
public int pages { get; set; }
public string per_page { get; set; }
public int total { get; set; }
}

public class LandBodyObject
{
public string id { get; set; }
public string iso2Code { get; set; }
public string name { get; set; }
public Region region { get; set; }
public Adminregion adminregion { get; set; }
public Incomelevel incomeLevel { get; set; }
public Lendingtype lendingType { get; set; }
public string capitalCity { get; set; }
public string longitude { get; set; }
public string latitude { get; set; }
}

public class Region
{
public string id { get; set; }
public string value { get; set; }
}

public class Adminregion
{
public string id { get; set; }
public string value { get; set; }
}

public class Incomelevel
{
public string id { get; set; }
public string value { get; set; }
}

public class Lendingtype
{
public string id { get; set; }
public string value { get; set; }
}

然后是反序列化方法。你的 Json 有两部分。所以我将它分成 2 部分进行反序列化。

    string result = "[{\"page\":1,\"pages\":1,\"per_page\":\"50\",\"total\":1},[{\"id\":\"BEL\",\"iso2Code\":\"BE\",\"name\":\"Belgium\",\"region\":{ \"id\":\"ECS\",\"value\":\"Europe & Central Asia(all income levels)\"},\"adminregion\":{ \"id\":\"\",\"value\":\"\"},\"incomeLevel\":{ \"id\":\"OEC\",\"value\":\"High income: OECD\"},\"lendingType\":{ \"id\":\"LNX\",\"value\":\"Not classified\"},\"capitalCity\":\"Brussels\",\"longitude\":\"4.36761\",\"latitude\":\"50.8371\"}]]";

var parts = result.Split(new[] {",["}, StringSplitOptions.None);
if (parts.Length > 1)
{
var header = parts[0].Replace("[", "");
var jsonHeader = JsonConvert.DeserializeObject<LandRootObject>(header);

var body = "[" + parts[1].Replace("]]","]");
var jsonBody = JsonConvert.DeserializeObject<List<LandBodyObject>>(body);
}

关于c# - JSON解析C#无名称变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34741903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com