1

I'm trying to deserialize this piece of json here with JSON.NET through an API, and I just want to extract the names that represent each array, which are John, Marie and Bob. Is it possible to extract just the names of each array with a foreach or any kind of method?

Here is how I currently have it:

WebRequest request = WebRequest.Create("...API URL...");
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);

string responseFromServer = reader.ReadToEnd();

reader.Close();
response.Close();
var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, dynamic>>(responseFromServer);


foreach (var item in dict["data"])
{
   string arrayName= Convert.ToString(item["data"]["?array_names?"]);
   Console.WriteLine(arrayName);
}

Data from JSON (responseFromServer)

 {"data": {
      "John": {
         "id": 266,
         "title": "Good old john",
         "name": "John Shepard",
         "key": "JS"
      },
      "Marie": {
         "id": 412,
         "title": "Helper Marie",
         "name": "Marie Godfather",
         "key": "MG"
      },
      "Bob": {
         "id": 23,
         "title": "Uncle Bob",
         "name": "Bob Plane",
         "key": "BP"
      }
}}

[EDIT: Added missing {} at start and end, sorry]

Thanks in advance

6
  • item["data"][0] may be? Commented Nov 22, 2016 at 11:15
  • 1
    At least post valid json Commented Nov 22, 2016 at 11:16
  • please try string arrayName= Convert.ToString(item[0]); Commented Nov 22, 2016 at 11:18
  • Json you show here does not have a array. Post the exact json. You cannot deserialize to Deserialize<Dictionary<string, dynamic>> you should have a repective object type in your code Commented Nov 22, 2016 at 11:28
  • Amit Kumar Ghosh and Anand Systematix -> Can not apply indexing with [] to an expression of type 'System.Collections.Generic.KeyValuePair <string, object>' Commented Nov 22, 2016 at 11:29

2 Answers 2

3

Assuming the valid json format - the follwing produces the result -

var json = "{'data':{'John':{'id':266,'title':'Good old john','name':'John Shepard','key':'JS'},'Marie':{'id':412,'title':'Helper Marie','name':'Marie Godfather','key':'MG'},'Bob':{'id':23,'title':'Uncle Bob','name':'Bob Plane','key':'BP'}}}";
var t = JObject.Parse(json)["data"];
foreach(JProperty j in t){
   Console.WriteLine(j.Name);
} /*John
    Marie
    Bob*/

It is recommended to use JSON.Net for working with json data types.

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly it. Thank you very much!
-1
var json = "{'data':{'John':{'id':266,'title':'Good old john','name':'John Shepard','key':'JS'},'Marie':{'id':412,'title':'Helper Marie','name':'Marie Godfather','key':'MG'},'Bob':{'id':23,'title':'Uncle Bob','name':'Bob Plane','key':'BP'}}}";

var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, dynamic>>(json);

foreach (var item in dict["data"])
    {
        string arrayName = Convert.ToString(item.Key);
        Console.WriteLine(arrayName);
    }

// John
// Marie
// Bob

2 Comments

This is not what the OP asked for. And this will not print "John, Marie, Bob"
You can try the code, it's printing "John, Marie, Bob"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.