反序列化JSON格式的数据

时间:2013-02-20 09:46:12

标签: c# json serialization

我有一个JSON数据字符串,我通过webrequest在线检索。我试图解析字符串并将数据映射到我的数据结构,这是不起作用的......我之前也使用过相同的方法用于其他映射,到目前为止效果很好。但在这种特殊情况下,它不起作用..

这是我检索到的JSON格式字符串:

{"Item":{"FirstName":"Antônio","LastName":"da Silva","CommonName":null,"Height":"175","DateOfBirth":{"Year":"1978","Month":"6","Day":"13"},"PreferredFoot":"Left","ClubId":"1825","LeagueId":"20","NationId":"54","Rating":"70","Attribute1":"53","Attribute2":"68","Attribute3":"74","Attribute4":"73","Attribute5":"55","Attribute6":"56","Rare":"1","ItemType":"PlayerM"}}

这是我的解析字符串的类:

public class ParsePlayerInfo
{
    private PlayerClass playerInfo;

    public PlayerClass parse(string stringToParse)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        if ((stringToParse != null) && (stringToParse.Length > 0))
        {
            this.playerInfo = serializer.Deserialize<PlayerClass>(stringToParse);
        }
        return this.playerInfo;
    }
}

这是我的Player类:

public class PlayerClass
{
    public PlayerClass()
    {
        this.Player = new PlayerContents();
    }

    public PlayerContents Player { get; set; }
}

这是我的玩家内容:

public class PlayerContents
{
    public PlayerContents()
    {
        string str;
        this.ItemType = str = "";
        this.Rare = str = str;
        this.Attribute6 = str = str;
        this.Attribute5 = str = str;
        this.Attribute4 = str = str;
        this.Attribute3 = str = str;
        this.Attribute2 = str = str;
        this.Attribute1 = str = str;
        this.Rating = str = str;
        this.NationId = str = str;
        this.LeagueId = str = str;
        this.ClubId = str = str;
        this.PreferredFoot = str = str;
        this.Height = str = str;
        this.CommonName = str = str;
        this.FirstName = this.LastName = str;
        this.DateOfBirth = new DOB();
    }

    public string Attribute1 { get; set; }

    public string Attribute2 { get; set; }

    public string Attribute3 { get; set; }

    public string Attribute4 { get; set; }

    public string Attribute5 { get; set; }

    public string Attribute6 { get; set; }

    public string ClubId { get; set; }

    public string CommonName { get; set; }

    public DOB DateOfBirth { get; set; }

    public string FirstName { get; set; }

    public string Height { get; set; }

    public string ItemType { get; set; }

    public string LastName { get; set; }

    public string LeagueId { get; set; }

    public string NationId { get; set; }

    public string PreferredFoot { get; set; }

    public string Rare { get; set; }

    public string Rating { get; set; }
}

编辑:

这是我的DOB课程:

public class DOB
{
    public DOB()
    {
        this.Year = this.Month = this.Day;
    }

    public string Day { get; set; }

    public string Month { get; set; }

    public string Year { get; set; }
}

任何想法?

1 个答案:

答案 0 :(得分:1)

尝试重命名Player属性以匹配JSON中的属性,即Item