-1

May i know how to parsing the JSON stated as below..... the JSON is part of Yahoo OAuth Contacts list.

JSON:

"fields":[{                 
                "id":2,
                "type":"nickname",
                "value":"Hello"
            },
            {
                "id":3,
                "type":"email",
                "value":"[email protected]"
            },
            {       
                "id":1,
                "type":"name",
                "value":{
                    "givenName":"Otopass",  
                    "middleName":"Test",
                    "familyName":"Hotmail"
                    },
            }],

C# object :

    private class fields 
    {
        public string id { get; set; }
        public string type { get; set; }
        //public string value { get; set; }                        //Stuck At Here !!!!
        //public Dictionary<string, string> value { get; set; }    //Stuck At Here !!!!
    }  

How to parse the "value"?? since it's combination type of String & Dictionary.

4
  • Check out similar question stackoverflow.com/questions/6416950/… Commented Jun 4, 2013 at 17:23
  • I don't know much about parsing json, but I can tell you that you can't have two fields in the same class with the same name, so having both a string and Dictionary named value in your fields class is going to throw a compiler error. Commented Jun 4, 2013 at 17:24
  • You may want to look into a JSON deserializer that supports the dynamic type. Commented Jun 4, 2013 at 17:30
  • @wgraham, yes... i'm looking for it now Commented Jun 4, 2013 at 17:41

1 Answer 1

0

I can't answer it using JavaScriptSerializer but you can do it using json.Net and Linq

var jObj = JObject.Parse(json);
var fields = jObj["fields"]
                .Select(x => new Field
                {
                    Id = (int)x["id"],
                    Type = (string)x["type"],
                    Value = x["value"] is JValue 
                            ? new Dictionary<string,string>(){{"",(string)x["value"]}}
                            : x["value"].Children()
                                        .Cast<JProperty>()
                                        .ToDictionary(p => p.Name, p => (string)p.Value)
                })
                .ToList();


private class Field
{
    public int Id { get; set; }
    public string Type { get; set; }
    public Dictionary<string, string> Value { get; set; }    
} 

PS: I fixed your partial json string as

string json = 
@"{""fields"":[
    {                 
        ""id"":2,
        ""type"":""nickname"",
        ""value"":""Hello""
    },
    {
        ""id"":3,
        ""type"":""email"",
        ""value"":""[email protected]""
    },
    {       
        ""id"":1,
        ""type"":""name"",
        ""value"":{
            ""givenName"":""Otopass"",  
            ""middleName"":""Test"",
            ""familyName"":""Hotmail""
            }
    }
]}";
Sign up to request clarification or add additional context in comments.

3 Comments

oh.. Thanks... I trying it right now. Is jObj = Newtonsoft.Json.Linq ??? It doesn't contain a definition for Select
@user2402624 All you need is using Newtonsoft.Json; and using Newtonsoft.Json.Linq;
Great!! I do some modification on your code and it's work for me... Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.