0

I have a JSON string, and I need to extract values from it- for example I need to get the value for ID and Name.

string someJson = @"[ {'ID': '12'} , { 'Name' : 'JAMES'} ]"; 

Note: I don't have a model created for this JSON.

My code:

string someJson = @"[ {'ID': '12'} , { 'Name' : 'JAMES'} ]"; 

List<object> json = JsonConvert.DeserializeObject<List<object>>(someJson);


Console.WriteLine("json count ", json[0]["ID"]);

The console.write doesn't print ID or can print Name. How can I solve this ? I hope I explained the question well, Sorry I am a newbie.

maccettura
  • 10,514
  • 3
  • 28
  • 35
Illep
  • 16,375
  • 46
  • 171
  • 302
  • Does that even compile? – James Thorpe Aug 15 '18 at 16:20
  • 1
    Possible duplicate of [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – Gene Aug 15 '18 at 16:23
  • Without `["ID"]` it does compile. – Illep Aug 15 '18 at 16:24
  • @Illep you _should_ create a model for this. Strongly C# is a strongly typed language, play to its strengths! Also, your Json seems very wrong to me. Its an array of objects but all the objects are different? Why would it be structured like that? Is this something you control? – maccettura Aug 15 '18 at 16:27
  • @maccettura I do not agree at all, using lists and dictionaries is very generic (obviously) and flexible, and you can handle unkown or not fixed json. Javascript is NOT strongly typed but that is where it comes from and often some guy will just add a field to that JSON and that should not break your server. (-J-ava-S-cript-O-bject-Notation-) – Martin Meeser Aug 15 '18 at 16:48
  • @Gene that is just one possible solution of the question – Martin Meeser Aug 15 '18 at 16:49
  • `Console.WriteLine("json count ", json[0]["ID"]);` this will never print anything else then 'json count ' – Martin Meeser Aug 15 '18 at 16:52

2 Answers2

1

Parse to a List<Dictionary<string, object>>

Check this example from JSON.NET.

Your example would look like this:

string someJson = @"[ {'ID': '12'} , { 'Name' : 'JAMES'} ]";
List<Dictionary<string, string>> student = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(someJson );
object val = student[0]["ID"];
Console.WriteLine($"json count {val.ToString()}");
Martin Meeser
  • 2,784
  • 2
  • 28
  • 41
  • It doesn't print the value. – Illep Aug 15 '18 at 16:33
  • ok now it does (checked), the output line from your question is wrong in the first place (seems updated, or I mistyped) – Martin Meeser Aug 15 '18 at 16:44
  • @MartinMeeser So absolutely no mention of the JSON structure's weirdness? Just solving the perceived problem is not the most helpful, sometimes there are bigger problems at play. This is likely an XY Problem, especially if the OP controls the Json – maccettura Aug 15 '18 at 16:51
  • 1
    @maccettura I find having the jsons flexible is great and very much looking at the big picture, but hey, that discussion just belongs to https://softwareengineering.stackexchange.com/, here at SO: concrete question, concrete answer, thats 100% ok – Martin Meeser Aug 15 '18 at 17:00
1

You could use deserialize into a JArray

    string someJson = @"[ {'ID': '12'} , { 'Name' : 'JAMES'} ]"; 

    var json = JsonConvert.DeserializeObject<JArray>(someJson);

    Console.WriteLine("json count " + json[0]["ID"]);
Kevin Smith
  • 13,746
  • 4
  • 52
  • 77