Read JSON Data in C#.net

chelsea17
chelsea17 used Ask the Experts™
on
I am pulling data from an API and I believe it is returned as a JSON string, how can I read certain data from the string ?  There are rows of data I want to loop through and put in a datatable, but can not figure out how to extract the data.

I have attached 3 files - the first 2 is the data I want to access, the third is the name of the columns.

Path to Column NamesWhere the data is - Label and Value are two fields inside I want to access.Path To Data
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
check a look to that online tools : http://json2csharp.com/
I will create classes reprensenting your JSON object to use with : https://github.com/JamesNK/Newtonsoft.Json/

for example :
[{"ok":"lol","ik":[{"a":1},{"a":1},{"a":4}]}]

Open in new window

give :
public class Ik
{
    public int a { get; set; }
}

public class RootObject
{
    public string ok { get; set; }
    public List<Ik> ik { get; set; }
}

Open in new window

Author

Commented:
once i create the class how to call the values i want ?
Multitechnician
Top Expert 2014
Commented:
in my example, if I want the ok value (which is "lol")

                string strReportData = "[{\"ok\":\"lol\",\"ik\":[{\"a\":1},{\"a\":1},{\"a\":4}]}]";
                JsonSerializer serializer = new JsonSerializer();

                RootObject reportData = (RootObject)serializer.Deserialize(strReportData, typeof(RootObject));

                string strOK = reportData.ok; // "lol"

Open in new window

Author

Commented:
i get the error - Argument 1: cannot convert from 'string' to 'System.IO.TextReader'      

on this Deserialize(reportData, typeof(Rootobject));


this is the code

            var reportData = restResponse.Content;

            JsonSerializer serializer = new JsonSerializer();

            Rootobject datafromreport = (Rootobject)serializer.Deserialize(reportData, typeof(Rootobject));

            string strOK = datafromreport.allData.ToString();

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial