P. Debrabant
P. Debrabant

Reputation: 171

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

Upvotes: 0

Views: 175

Answers (5)

P. Debrabant
P. Debrabant

Reputation: 171

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.

Upvotes: 0

P.Petkov
P.Petkov

Reputation: 1579

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

Upvotes: 0

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

Upvotes: 1

Arjun Arora
Arjun Arora

Reputation: 1006

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

Upvotes: 0

zchpit
zchpit

Reputation: 3121

The JSON input is valid according to RFC 4627 (JSON specfication).

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

Upvotes: 0

Related Questions