Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I once retrieve a specific value from JSON formatted text before but now my approach seemed to be not working. My string sp value is receiving null value. I tried to look for the same question with an answer here on codeproject but none of them works.

I was trying to get the "id" from

{"data": [
    {
      "id": 109993,
      "member_id": 378832,
      "issuer_sign": 9,
      "ta_id": 96122,
      "endorsement": null,
      "title": "Member"
      
}
]
}


What I have tried:

dynamic obj = JsonConvert.DeserializeObject(str);
     string sp = Convert.ToString(obj.id);
Posted
Updated 2-Jul-18 0:30am

You need to create a custom object that includes the same signature as your JSON. Something like-
public class MyData
{
    public int id { get; set; }
    public int member_id { get; set; }
    public int issuer_sign { get; set; }
    public int ta_id { get; set; }
    public string endorsement { get; set; }
    public string title { get; set; }
}

var data = JsonConvert.DeserializeObject<MyData>(jsonString);

And then deserialize your JSON with this object. Please read this for more reference-
Deserialize JSON with C# - Stack Overflow[^]
How to: Serialize and Deserialize JSON Data | Microsoft Docs[^]

KR
 
Share this answer
 
Comments
Maciej Los 2-Jul-18 3:01am    
5ed!
Your json is a property called "data" which is an array of objects, and in your case there is only one item in the array. If you wanted to get the id of the first item in the data array you would do this

string sp = Convert.ToString(obj.data[0].id);


However if you expect more items to be in data then you'd need to loop to find the one you wanted, or to process them all, whichever meets your requirements.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900