简体   繁体   中英

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

With C#, I try without success to de-serialize this json content :

{
    "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:

[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):

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).

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

JSON sturcture is based on Key Value pair. Correct JSON format is like:

{
"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

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. 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.

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; tag's object count is 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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