1

My test json is as below..

 string json = "[{Name:'John Simith',Age:35},{Name:'Pablo Perez',Age:34}]"; 

The json could have any key values and so I do not have a class to deserialize it with.. I can deserialize it as IEnumerable as below

IEnumerable<dynamic> data = JsonConvert.DeserializeObject<IEnumerable<dynamic>>(json);

I need to convert it to 2D object arrays : object[,] as below..

[
['John Simith',35],['Pablo Perez',34]
]

Any help is sincerely appreciated. Thanks

Arnab
  • 2,324
  • 6
  • 36
  • 60
  • http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net? – CodeCaster Sep 18 '16 at 10:58

2 Answers2

1

You can change from dynamic to IDictionary<string, object> in order to be able to enumerate unknown keys. Then a LINQ expression can convert it to an array, like this:

var json = "[{Name:'John Simith',Age:35},{Name:'Pablo Perez',Age:34}]";
var data = JsonConvert.DeserializeObject<IEnumerable<IDictionary<string, object>>>(json);

var array = data.Select(d => d.Values.ToArray()).ToArray();
Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
0

If you don't have a C# class to deserialize it to, the best thing to do is to use the dynamic LINQ objects that JSON.NET provides, meaning JObject, JArray and so on. This lets you query the structure dynamically, read types values and so on, without having to fight with C#'s type system:

http://www.newtonsoft.com/json/help/html/ParsingLINQtoJSON.htm

It would be much easier to traverse the JSON tree and convert to object[,] from those objects.

Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63