gpt4 book ai didi

c# - JSON.net反序列化

转载 作者:行者123 更新时间:2023-11-30 13:27:31 26 4
gpt4 key购买 nike

我有一个 JSON 编码的字符串。我也在上 C# 课:

class Pupu{
public static List<Pupu> plist = new List<Pupu>();
public string user {get; set;}
public string number {get; set;}
}


[{"user":"27FFBADD7284E5CB98EAC45559589E28FDDDC3AD","number":"62827"},{"user":"27FFBADD7284E5CB98EAC45559589E28FDDDC3AD","number":"30460"}, {"user":"0D27D44D40C5185423078B3C93B3E6B596AD21A0","number":"25143"}, {"user":"0D27D44D40C5185423078B3C93B3E6B596AD21A0","number":"22776"}, {"user":"27FFBADD7284E5CB98EAC45559589E28FDDDC3AD","number":"19755"}]

我正在反序列化:

 plist = JsonConvert.DeserializeObject<List<Pupu>>(stringAbove);

我不断得到:

 Attempt to access the method failed: System.Collections.Generic.List`1..ctor();

谢谢,我已经找了几个小时了。 (我是 C# 的新手)。

最佳答案

让我们尝试清理您的代码:

public class Score
{
public string user { get; set; }
public string number { get; set; }
}

现在您可以将 JSON 反序列化为分数列表:

List<Score> plist = JsonConvert.DeserializeObject<List<Score>>(stringAbove);

这是一个完整的例子:

public class Score
{
public string user { get; set; }
public string number { get; set; }
}

class Program
{
static void Main()
{
var json = "[{\"user\":\"27FFBADD7284E5CB98EAC45559589E28FDDDC3AD\",\"number\":\"62827\"},{\"user\":\"27FFBADD7284E5CB98EAC45559589E28FDDDC3AD\",\"number\":\"30460\"}, {\"user\":\"0D27D44D40C5185423078B3C93B3E6B596AD21A0\",\"number\":\"25143\"}, {\"user\":\"0D27D44D40C5185423078B3C93B3E6B596AD21A0\",\"number\":\"22776\"}, {\"user\":\"27FFBADD7284E5CB98EAC45559589E28FDDDC3AD\",\"number\":\"19755\"}]";
List<Score> plist = JsonConvert.DeserializeObject<List<Score>>(json);

foreach (Score score in plist)
{
Console.WriteLine("user: {0}, number: {1}", score.user, score.number);
}
}
}

关于c# - JSON.net反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10534576/

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