Ghostfire
Ghostfire

Reputation: 155

How do I deserialize this JSON to access each piece of data in it?

How do I deserialize this JSON?

{"totalResult":3,"categories":[{"1":{"categoryName":"plumbers","categoryIconUrl":"","headingName":"plumbers"}},{"2":{"categoryName":"plumbing parts and accessories","categoryIconUrl":"","headingName":"plumbing parts and accessories"}},{"3":{"categoryName":"plumbing services","categoryIconUrl":"","headingName":"plumbing services"}}]} 

So I have tried a couple of things already... Currently I can get the "totalResult", however I can not get the other data, here is the data contract that I use to get the information I am currently getting:

[DataContract]
public class categories_result
{
    [DataMember(Name="totalResult")]
    public int totalResult {get; set;}
    [DataMember(Name="categories")]
    public category[] results {get; set;}
}
[DataContract]
public class category
{
    [DataMember(Name="categoryName")]
    public string categoryName {get; set;}
    [DataMember(Name="categoryIconUrl")]
    public string categoryIconUrl {get; set;}
    [DataMember(Name="headingName")]
    public string headingName {get; set;}
}

When it comes to the category class, returned as results, I get the following

-       results {TrudonMobile.category[3]}  TrudonMobile.category[]
-       [0] {WP7proj.category}  WP7proj.category
    categoryIconUrl Could not evaluate expression   string
    categoryName    Could not evaluate expression   string
    headingName Could not evaluate expression   string

It feels like I am missing something but I am not sure.

ADDED:

I thought I might add the code where am actually trying to deserialize as this could be useful :>

            try
            {
                Stream responseStream = e.Result;
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(categories_result));
                categories_result response = (categories_result)ser.ReadObject(responseStream);

            }
            catch (Exception x)
            {
                string bob = x.ToString();
                return;
            }

Upvotes: 2

Views: 359

Answers (2)

L.B
L.B

Reputation: 116108

Using Json.Net

string json =
@"
{
    ""totalResult"": 3,
    ""categories"": [
        {
            ""1"": {
                ""categoryName"": ""plumbers"",
                ""categoryIconUrl"": """",
                ""headingName"": ""plumbers""
            }
        },
        {
            ""2"": {
                ""categoryName"": ""plumbing parts and accessories"",
                ""categoryIconUrl"": """",
                ""headingName"": ""plumbing parts and accessories""
            }
        },
        {
            ""3"": {
                ""categoryName"": ""plumbing services"",
                ""categoryIconUrl"": """",
                ""headingName"": ""plumbing services""
            }
        }
    ]
}
";

dynamic jObj = JsonConvert.DeserializeObject(json);

for (int i = 0; i < jObj.categories.Count; i++)
{
    Console.WriteLine(jObj.categories[i][(i+1).ToString()].categoryName);
}

//OR

JObject jObj = (JObject)JsonConvert.DeserializeObject(json);

for (int i = 0; i < jObj["categories"].Count(); i++)
{
    Console.WriteLine(jObj["categories"][i][(i+1).ToString()]["categoryName"]);
}

Upvotes: 2

SENTHIL KUMAR
SENTHIL KUMAR

Reputation: 647

Try json.newtonsoft library

       string data = "@:{"totalResult":3,"categories":[{"1":{"categoryName":"plumbers","categoryIconUrl":"","headingName":"plumbers"}},{"2":{"categoryName":"plumbing parts and accessories","categoryIconUrl":"","headingName":"plumbing parts and accessories"}},{"3":{"categoryName":"plumbing services","categoryIconUrl":"","headingName":"plumbing services"}}]} ";


        JObject o = JObject.Parse(data);

        JArray alldata = (JArray)(o["category"]);

to access the members in the category class

       JObject first = (JObject)category[1];

Upvotes: 1

Related Questions