如何用C#反序列化json?

时间:2015-03-18 10:48:57

标签: c# json list object

使用C#,我没有成功地尝试反序列化这个json内容:

{
    "code":200,
    "message":"OK",
    "name":"The name",
    "description":"The description",
    "tags":{
        "0.1.3":{
            "date":"2015-03-11",
            "author":"SOMEONE",
        },
        "0.1.2":{
            "date":"2015-03-11",
            "author":"SOMEONE",
        }
    }
}

你注意到,有一个"标签"的列表对象,但我没有桌子。

目标类的开头:

[DataContract]
public class Project
{
    [DataMember]
    public int code { get; set; }
    [DataMember]
    public string message { get; set; }
    [DataMember]
    public string name { get; set; }
    [DataMember]
    public string description { get; set; }

    [DataMember]
        **How can I handle tags entries ?**
}

[DataContract]
public class Tag
{
    [DataMember]
    public string date { get; set; }

    [DataMember]
    public string author { get; set; }
}

5 个答案:

答案 0 :(得分:1)

如果您正在使用JSON.NET,那么您可以拥有以下内容:

[DataContract]
public class Project
{
    [DataMember]
    public int code { get; set; }
    [DataMember]
    public string message { get; set; }
    [DataMember]
    public string name { get; set; }
    [DataMember]
    public string description { get; set; }

    [DataMember]
    public Dictionary<string, Tag> tags { get; set; }
}

[DataContract]
public class Tag
{
    [DataMember]
    public string date { get; set; }

    [DataMember]
    public string author { get; set; }
}

然后您将使用以下方式(假设responseString包含您的JSON):

Project project = JsonConvert.DeserializeObject<Project>(responseString);

foreach (KeyValuePair<string, Tag> tag in project.tags)
{
    Debug.WriteLine("Version: {0}", tag.Key);
    Debug.WriteLine("Date: {0}", tag.Value.date);
    Debug.WriteLine("Author: {0}", tag.Value.author);
}

答案 1 :(得分:0)

根据RFC 4627(JSON规范),JSON输入有效。

http://www.freeformatter.com/json-validator.html

答案 2 :(得分:0)

JSON结构基于Key Value对。正确的JSON格式如下:

{
"object":{
    "DataMember1":"String content",
    "DataMember2":"String content",
    "DataMember3":"String content"
},
"object2":{
    "DataMember1":"String content",
    "DataMember2":"String content"
}

}

转到basics

要检查您的json结构,您可以从click here

进行验证

答案 3 :(得分:0)

嗯,我能够反序列化:

Newtonsoft.Json.JsonConvert.DeserializeObject<Project>(json);

但是,这是我的班级定义:

 public class Project
{

    public string code { get; set; }

    public string message { get; set; }

    public string name { get; set; }

    public string description { get; set; }

    public Dictionary<string,Tag> tags { get; set; }
}


public class Tag
{

    public string date { get; set; }

    public string author { get; set; }
}

答案 4 :(得分:0)

感谢ArturoTorresSánchez。要获取“标记”条目,声明必须是:

[DataContract]
public class Project
{
    [DataMember]
    public int code { get; set; }
    [DataMember]
    public string message { get; set; }
    [DataMember]
    public string name { get; set; }
    [DataMember]
    public string description { get; set; }
    [DataMember]
    public Dictionary<string, Tag> tags { get; set; }
}

最重要的是,我必须修改默认设置,并在创建DataContractJsonSerializer实例时使用新设置。

DataContractJsonSerializerSettings settings = 
            new DataContractJsonSerializerSettings();
settings.UseSimpleDictionaryFormat = true;

DataContractJsonSerializer serializer = 
            new DataContractJsonSerializer(typeof(Project), settings);

Project results = (Project)serializer.ReadObject(ms);

没有 settings.UseSimpleDictionaryFormat = true; 标记的对象计数为0。