I
I
IPv42015-12-24 21:53:03
JSON
IPv4, 2015-12-24 21:53:03

How to use json array in C# received via WebRequests?

There is an array:

{"items":[{"key":1,"val":"one"},{"key":2,"val":"two"},{"key":3,"val":"three"},{"key":4,"val":"four"}]}

Please show me an example with which I can deserialize an array and use it like this:
//data - десериализованный json массив
foreach(string value in data) {
  print("ID: " + value.key + ", TEXT: " + value.val);
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Arxont, 2015-12-25
@IPv4

Something like this -

static void Main()
{
  var afterWebRequests = "{\"items\":[{\"key\":1,\"val\":\"one\"},{\"key\":2,\"val\":\"two\"},{\"key\":3,\"val\":\"three\"},{\"key\":4,\"val\":\"four\"}]}";
  var temp = (MyArray)JsonConvert.DeserializeObject(afterWebRequests, typeof(MyArray));

  foreach(Item item in temp.items)
  {
    Console.WriteLine("Key: {0}, Value {1}", item.key, item.val);
  }
  Console.ReadKey();
}

public class MyArray
{
  public List<Item> items { get; set; }
}

public class Item
{
  public int key { get; set; }
  public string val { get; set; }
}

D
Dmitry Kovalsky, 2015-12-24
@dmitryKovalskiy

stackoverflow.com/questions/17038810/newtonsoft-js... Actually, the name of the required library and an example of use for your task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question