3

我有一个 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# 很陌生)。

4

1 回答 1

12

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

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);    
        }
    }
}
于 2012-05-10T13:05:05.080 回答