A
A
Alex Wells2015-10-18 15:31:40
JSON
Alex Wells, 2015-10-18 15:31:40

How to get value from JSON array?

Hello. There is an array returned by one API, here it is: pastebin.com/7atf5CzT
I need to get each 'value' for each name (key), for example AK-47 | Aquamarine Revenge (Battle-Scarred) => 1420. I use newtonsoft.json library for parsing. I've already tried this and that, but it doesn't work. What's wrong?

dynamic decoded = JsonConvert.DeserializeObject(response.Body);
                decoded = decoded.response;
                foreach (dynamic item in decoded.items)
                {
                    Console.WriteLine(item.value); // RuntimeBinderException: JProperty does not contain a definition for 'value'
                    Console.WriteLine(item[2]);  // InvalidOperationException: cannot access child value on JProperty
                    Console.WriteLine(item.Name.ToString()); // работает отлично
                }

Actually key gets perfectly, and value - well in any way. Neither .value, nor by index, nor ["value"] works in any way! I even tried .GetValue("value") - it doesn't work either. And yes - I'm pretty sure it definitely contains 'value', since I was displaying item in its entirety and it was there.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dasha Tsiklauri, 2015-10-18
@Alex_Wells

dynamic obj = JsonConvert.DeserializeObject(json);
            var resp = obj.response;
            foreach (JProperty item in resp.items)
            {
                var token = item.FirstOrDefault();
                if (token != null)
                    Console.WriteLine(token["value"]);
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question