0

I am trying to read json object contain some values in JsonArray using Json.NET library. I want to get each array value into variables. Following is the Json String. Please guide, thanks.

{"enad_list":
      [{
        "et_cnic":"1090202369011",
        "et_name":"Its me",
        "et_father_husband_name":"My father name",
        "et_present_add":"Its my address",
        "et_permanent_add":"Its my another address ",
        "et_date_of_birth":"10-9-1982",
        "et_birth_place":"My Birth place",
        "et_expiry_date":"2020-12-15"
       }]
}

Edit: So far i have tried this code and getting an error. Please see image for error

    string jsonData =@"{""enad_list"":[{""et_cnic"":""1090202369011"",""et_name"":""Its me""}]}";
    dynamic jsonData = JObject.Parse(abc.ToString());
    int cnic = jsonData.enad_list.et_cnic;

Error Image enter image description here

mpromonet
  • 11,326
  • 43
  • 62
  • 91
Rauf Abid
  • 346
  • 1
  • 8
  • 17
  • 3
    http://www.newtonsoft.com/json has examples easy peasy – online Thomas Dec 20 '16 at 09:51
  • 2
    What is the question? Are you asking which method to use to deserialize this string? Have you checked the documentation or simply googled for this? Did you try something that failed? – Panagiotis Kanavos Dec 20 '16 at 09:52
  • This answer is what you need I guess: http://stackoverflow.com/questions/4749639/deserializing-json-to-net-object-using-newtonsoft-or-linq-to-json-maybe/10718128#10718128 – TheShad0w_ Dec 20 '16 at 09:54
  • @Panagiotis, yes i need to get each array value into variables whichever method we use, thanks. – Rauf Abid Dec 20 '16 at 09:59

1 Answers1

9

Try something like this for multiple array values

dynamic jsonData = JsonConvert.DeserializeObject<dynamic>(your_json)
int cnic = jsonData.enad_list[0].et_cnic;

OR for single string

dynamic jsonData = JObject.Parse(your_json-string); 
int cnic = jsonData.et_cnic;
مسعود
  • 679
  • 10
  • 25
  • Please see Edit portion of my original thread. I facing error while using your code, thanks. – Rauf Abid Dec 20 '16 at 10:32
  • Thanks i got it. Its working now. Just need little explanation that what does it mean to DeserializeObject here? while we get only from single string(your second portion of code)?. Sorry new to Json, thanks. – Rauf Abid Dec 20 '16 at 10:39
  • DeserializeObject Deserializes the JSON to a .NET object. JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation. – مسعود Dec 20 '16 at 12:54
  • 1
    Thanks Dear Masood. – Rauf Abid Dec 21 '16 at 04:21