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

1

2 Answers 2

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(); 
1
  • hello. but how to preserve the property names? if I use .toDictionary(), then how?CommentedJan 24, 2018 at 7:22
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.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.