-2

I am trying to consume an API service that will give me some data back in JSON that will then be used in a winForm Project.

I Used the following backend code:

        var json = new WebClient().DownloadString (sURL);
        var results = JsonConvert.DeserializeObject(json);

        dynamic array = JArray.Parse(json);

reulting in the following data:

    {
        {"AppId": 1, 
        "applications": "C:\\Program Files (x86)\\Tencent\\WeChat\\WeChat.exe", 
        "recordId": 1,            
        "userId": 1}
    }

So I expected I could use array["applications"] to get my data but it turned out i was wrong. Any help is appreciated.

Rodney Wormsbecher
  • 897
  • 2
  • 17
  • 39
  • 1
    This JSON is not valid ! – mybirthname Nov 01 '16 at 16:39
  • you have to deserialize it to a object and then use it..Create a class with Appid and applications and deserialise the results array to its class..this can be helpful to u http://stackoverflow.com/questions/18192357/deserializing-json-object-array-with-json-net – Geeky Nov 01 '16 at 16:40
  • Deserialization to object is not mandatory, give proper json. – mybirthname Nov 01 '16 at 16:42

2 Answers2

2

After using James his answer I was able to do the following to solve my problem. both in a loop as well as a single variable.

        var resulti = "";
        JArray app = JArray.Parse(json);

        // single var
        var tester = app[0]["applications"];

        // loop
        foreach (var item in app)
        {
            resulti += item["applications"];
        }
Rodney Wormsbecher
  • 897
  • 2
  • 17
  • 39
1

assuming if this was your json:

[
{"AppId": 1, 
"applications": "C:\\Program Files (x86)\\Tencent\\WeChat\\WeChat.exe", 
"recordId": 1,            
"userId": 1}
]

you can use JArray a= JArray.Parse(json); and loop through.

James
  • 729
  • 6
  • 10