Get values ​​from json string

I have a json string . I want to get contactfrom json string. After json contains contacts array. here is my json line.

{
   "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",
               }
            ]
         }
    }
}

Here I want to get the values givenName,familyName,email. How can I get values ​​from json string.

Note: there is an array in json contact. I sent only one contact from this json.

I tried something like this. But did not work.

JObject json = JObject.Parse(returnStr);
JArray fields = (JArray)json["contacts"]["contact"]["fields"][0];
JArray FValues = (JArray)json["contact"]["fields"]["value"];

I tried this

public class Field
    {
        public int id { get; set; }
        public string type { get; set; }
        public object value { get; set; }
        public string editedBy { get; set; }
        public List<object> flags { get; set; }
        public List<object> categories { get; set; }
        public string updated { get; set; }
        public string created { get; set; }
        public string uri { get; set; }
        public bool? isConnection { get; set; }
    }

    public class contact
    {
        public bool isConnection { get; set; }
        public int id { get; set; }
        public List<Field> fields { get; set; }
        public List<object> categories { get; set; }
        public int error { get; set; }
        public int restoredId { get; set; }
        public string created { get; set; }
        public string updated { get; set; }
        public string uri { get; set; }
    }

    public class Contacts
    {
        public List<contact> contact { get; set; }
        public int count { get; set; }
        public int start { get; set; }
        public int total { get; set; }
        public string uri { get; set; }
        public bool cache { get; set; }
    }

    public class RootObject
    {
        public Contacts contacts { get; set; }
    }

and

JavaScriptSerializer serializer1 = new JavaScriptSerializer();
RootObject obje = serializer1.Deserialize<RootObject>(returnStr);

But that gives me a 0 value in obje.

0
source share
5 answers
  • First make sure your Json is in a valid format using jsonlint

  • Then generate the class base using json2csharp

    public class Field
    {
        public int id { get; set; }
        public string type { get; set; }
        public object value { get; set; }
        public string editedBy { get; set; }
        public List<object> flags { get; set; }
        public List<object> categories { get; set; }
        public string updated { get; set; }
        public string created { get; set; }
    }
    
    public class Contact
    {
        public bool isConnection { get; set; }
        public int id { get; set; }
        public List<Field> fields { get; set; }
    }
    
    public class Contacts
    {
        public List<Contact> contact { get; set; }
    }
    
    public class RootObject
    {
        public Contacts contacts { get; set; }
    }
    
  • Newtonsoft JSON Json , .

    JsonConvert.DeserializeObject<RootObject>(string json);
    
+3

json ( http://jsonutils.com/ ):

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<object> flags { get; set; }
    public IList<object> 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<Field> fields { get; set; }
}

public class Contacts
{
    public IList<Contact> contact { get; set; }
}

public class Example
{
    public Contacts contacts { get; set; }
}

Deserialization (, , System.Web.Extensions):

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

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"
                    }
                ]
            }
        ]
    }
}
+3

:

public class Contact
    {
        public bool isConnection { get; set; }
        public int id { get; set; }
        public List<Field> fields { get; set; }
    }
public class Field
{
    public int id { get; set; }
    public string type { get; set; }
    public object value { get; set; }
    public string editedBy { get; set; }
    public string[] flags { get; set; }
    public string[] categories { get; set; }
    public DateTime updated { get; set; }
    public DateTime created { get; set; }
}

public class Name
{
    public string givenName { get; set; }
    public string middleName { get; set; }
    public string familyName { get; set; }
    public string prefix { get; set; }
    public string suffix { get; set; }
    public string givenNameSound { get; set; }
    public string familyNameSound { get; set; }
}

LINQ .

+1

JObject , , http://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing

dynamic contacts = (JArray)json["contacts"]
foreach(dynamic contact in contacts.contact) {
   // look at the fields... 
}

PS. , VS,

0

-, , , JSON . "" , . JSON : http://jsonformatter.curiousconcept.com

-, . @grundy , JSON LINQ, Newtonsoft. .

: -

var json = JObject.Parse(returnStr);
var fields = (JArray)json["contacts"]["contact"][0]["fields"];

var givenName = fields[0]["value"]["givenName"];
var familyName = fields[0]["value"]["familyName"];
var email = fields[1]["value"];
0

All Articles