Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing JSON - how to ignore the root element?

Tags:

I'm consuming a WCF service that returns JSON results wrapped inside the 'd' root element. The JSON response looks like this:

{"d":[   {     "__type":"DiskSpaceInfo:#Diagnostics.Common",     "AvailableSpace":38076567552,     "Drive":"C:\\",     "TotalSpace":134789197824   },   {     "__type":"DiskSpaceInfo:#Diagnostics.Common",     "AvailableSpace":166942183424,     "Drive":"D:\\",     "TotalSpace":185149157376   } ]} 

I don't want to use dynamic typing, I have my class Diagnostics.Common.DiskSpaceInfo that I want to use when deserializing.

I am using Json.NET (Netwonsoft JSON).

The question is how to tell it to ignore the root element (that 'd' element) and parse what is inside.

The best solution I have so far is to use an anonymous type:

DiskSpaceInfo[] result = JsonConvert.DeserializeAnonymousType(json, new     {         d = new DiskSpaceInfo[0]     }).d; 

this actually works but I don't like it very much. Is there another way? What I would like is something like:

DiskSpaceInfo[] result = JsonConvert.Deserialize(json, skipRoot: true); 

or something like that...

like image 573
Nikolaos Georgiou Avatar asked Dec 04 '12 12:12

Nikolaos Georgiou


People also ask

How do I ignore a root element in JSON?

How to ignore parent tag from json?? String str = "{\"parent\": {\"a\":{\"id\": 10, \"name\":\"Foo\"}}}"; And here is the class to be mapped from json. (a) Annotate you class as below @JsonRootName(value = "parent") public class RootWrapper { (b) It will only work if and only if ObjectMapper is asked to wrap.

What is JSON root element?

According to the modified Backus-Naur-Form on the right side pane of http://json.org/ the root element of a JSON data structure can be any of these seven types/values: Object Array String Number true false null.

What is serialized and deserialized in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

How do I deserialize a JSON file?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.


1 Answers

If you know what to search like in this case "d" which is a root node then you can do the following.

JObject jo = JObject.Parse(json); DiskSpaceInfo[] diskSpaceArray = jo.SelectToken("d", false).ToObject<DiskSpaceInfo[]>(); 

If you simply want to ignore the root class which you do not know then you can use the "@Giu Do" solution just that you can use test2.ToObject<DiskSpaceInfo[]>(); instead of the Console.Write(test2);

        JObject o = JObject.Parse(json);         if (o != null)         {             var test = o.First;             if (test != null)             {                 var test2 = test.First;                 if (test2 != null)                 {                     DiskSpaceInfo[] diskSpaceArray = test2.ToObject<DiskSpaceInfo[]>();                 }             }         } 
like image 140
keyr Avatar answered Oct 08 '22 02:10

keyr