简体   繁体   English

如何使用C#反序列化此json?

[英]How can I de-serialize this json with C#?

With C#, I try without success to de-serialize this json content : 使用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",
        }
    }
}

You have noticed, there's a list of "tag" objects, but I have not a table. 您已经注意到,这里有一个“标签”对象列表,但是我没有表格。

Beginning of the target classes : 目标课程的开始:

[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; }
}

If you're using JSON.NET, then you can have the following: 如果使用的是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; }
}

Which then you would use the following way (assuming responseString contains your JSON): 然后,您将使用以下方式(假设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);
}

The JSON input is valid according to RFC 4627 (JSON specfication). 根据RFC 4627(JSON规范),JSON输入有效。

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

JSON sturcture is based on Key Value pair. JSON结构基于键值对。 Correct JSON format is like: 正确的JSON格式如下:

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

} }

Goto basics 转到基础

To check your json structure you can validate from click here 要检查您的json结构,您可以从此处进行验证

Hmm, I was able to deserialize: 嗯,我能够反序列化:

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

However, here is my class definition: 但是,这是我的类定义:

 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; }
}

Thanks to Arturo Torres Sánchez. 感谢Arturo TorresSánchez。 To get the "tag" entries, the declaration must be : 要获取“标签”条目,声明必须为:

[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; }
}

And the most important, I must modify the default settings and use the new settings when I create the instance of DataContractJsonSerializer. 最重要的是,在创建DataContractJsonSerializer实例时,必须修改默认设置并使用新设置。

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

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

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

Without the settings.UseSimpleDictionaryFormat = true; 没有设置。UseSimpleDictionaryFormat = true; tag's object count is 0. 标签的对象计数为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM