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

4
  • 3
    newtonsoft.com/json has examples easy peasy Commented Dec 20, 2016 at 9: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? Commented Dec 20, 2016 at 9:52
  • This answer is what you need I guess: stackoverflow.com/questions/4749639/… Commented Dec 20, 2016 at 9:54
  • @Panagiotis, yes i need to get each array value into variables whichever method we use, thanks. Commented Dec 20, 2016 at 9:59

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Please see Edit portion of my original thread. I facing error while using your code, thanks.
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.
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.
Thanks Dear Masood.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.