我有一个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);
}
}
}