-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":"MyTesting@hotmail.com"
            },
            {       
                "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.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
user2402624
  • 211
  • 1
  • 4
  • 10
  • Check out similar question http://stackoverflow.com/questions/6416950/serializing-dictionaries-with-javascriptserializer – Ondrej Svejdar Jun 04 '13 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. – Blake Hood Jun 04 '13 at 17:24
  • You may want to look into a JSON deserializer that supports the `dynamic` type. – wgraham Jun 04 '13 at 17:30
  • @wgraham, yes... i'm looking for it now – user2402624 Jun 04 '13 at 17:41

1 Answers1

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"":""MyTesting@hotmail.com""
    },
    {       
        ""id"":1,
        ""type"":""name"",
        ""value"":{
            ""givenName"":""Otopass"",  
            ""middleName"":""Test"",
            ""familyName"":""Hotmail""
            }
    }
]}";
I4V
  • 34,891
  • 6
  • 67
  • 79