Retrieve the values from json string

后端 未结 5 784
忘掉有多难
忘掉有多难 2021-01-26 05:33

I have json string. I want to retrieve the contact from json string. Following json contains array of contacts. here is my json string

5条回答
  •  灰色年华
    2021-01-26 05:50

    Class for json object (generated with http://jsonutils.com/ after correcting some syntax error):

    public class Field
    {
        public int id { get; set; }
        public string type { get; set; }
        public object value { get; set; }
        public string editedBy { get; set; }
        public IList flags { get; set; }
        public IList categories { get; set; }
        public DateTime updated { get; set; }
        public DateTime created { get; set; }
    }
    
    public class Contact
    {
        public bool isConnection { get; set; }
        public int id { get; set; }
        public IList fields { get; set; }
    }
    
    public class Contacts
    {
        public IList contact { get; set; }
    }
    
    public class Example
    {
        public Contacts contacts { get; set; }
    }
    
    
    

    Deserialization (you will probably need to add a reference to System.Web.Extensions):

    System.Web.Script.Serialization.JavaScriptSerializer deSer = new System.Web.Script.Serialization.JavaScriptSerializer();
    JSonPrintSettingsToXml.Input.Example deserializedJSON = deSer.Deserialize(yourJSON);
    

    Here is the corrected JSON

    {
        "contacts": {
            "contact": [
                {
                    "isConnection": false,
                    "id": 33554611,
                    "fields": [
                        {
                            "id": 33554748,
                            "type": "name",
                            "value": {
                                "givenName": "Jhon",
                                "middleName": "",
                                "familyName": "Scot",
                                "prefix": "",
                                "suffix": "",
                                "givenNameSound": "",
                                "familyNameSound": ""
                            },
                            "editedBy": "OWNER",
                            "flags": [],
                            "categories": [],
                            "updated": "2012-12-23T07:40:23Z",
                            "created": "2012-12-23T07:40:23Z"
                        },
                        {
                            "id": 33554749,
                            "type": "email",
                            "value": "someone@example.com",
                            "editedBy": "OWNER",
                            "flags": [],
                            "categories": [],
                            "updated": "2012-12-23T07:40:23Z",
                            "created": "2012-12-23T07:40:23Z"
                        }
                    ]
                }
            ]
        }
    }
    

    提交回复
    热议问题