Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Deserialize JSON data?

I am new to working with JSON data.

I am reading data from a web service. The query data sent back is the following:

[["B02001_001E","NAME","state"],  ["4712651","Alabama","01"],  ["691189","Alaska","02"],  ["6246816","Arizona","04"],  ["18511620","Florida","12"],  ["9468815","Georgia","13"],  ["1333591","Hawaii","15"],  ["1526797","Idaho","16"],  ["3762322","Puerto Rico","72"]] 

Is there a way to Deserialize this data in such a way that the base object will be generated without me first defining what the object is like? In the above example the object is defined by the first row:

           ["B02001_001E","NAME","state"], 

In general the web service will return the query data formatted as a two dimensional JSON array where the first row provides column names and subsequent rows provide data values.

like image 275
eitan barazani Avatar asked Aug 14 '13 21:08

eitan barazani


People also ask

What does it mean to deserialize a 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 JSON data in Python?

The default method of deserialization is json. loads() which takes a string as an input and outputs a JSON dictionary object. To convert the dictionary object to a custom class object, you need to write a deserialize method. The easiest way is to add a static method to the class itself.

What is deserialization of JSON in Java?

Deserialization is transforming the data from a file or stream back into an object to be used in your application. This can be binary data or structured data like JSON and XML. Deserialization is the opposite of serialization, which transforms objects into byte streams or structured text.

How does Newtonsoft JSON deserialize work?

Newtonsoft. Json uses reflection to get constructor parameters and then tries to find closest match by name of these constructor parameters to object's properties. It also checks type of property and parameters to match. If there is no match found, then default value will be passed to this parameterized constructor.


2 Answers

You can deserialize this really easily. The data's structure in C# is just List<string[]> so you could just do;

  List<string[]> data = JsonConvert.DeserializeObject<List<string[]>>(jsonString); 

The above code is assuming you're using json.NET.

EDIT: Note the json is technically an array of string arrays. I prefer to use List<string[]> for my own declaration because it's imo more intuitive. It won't cause any problems for json.NET, if you want it to be an array of string arrays then you need to change the type to (I think) string[][] but there are some funny little gotcha's with jagged and 2D arrays in C# that I don't really know about so I just don't bother dealing with it here.

like image 117
evanmcdonnal Avatar answered Sep 20 '22 13:09

evanmcdonnal


If you use .Net 4.5 you can also use standard .Net json serializer:

using System.Runtime.Serialization.Json; ...     Stream jsonSource = ...; // serializer will read data stream var s = new DataContractJsonSerializer(typeof(string[][])); var j = (string[][])s.ReadObject(jsonSource); 

In .Net 4.5 and older you can use JavaScriptSerializer class:

using System.Web.Script.Serialization; ... JavaScriptSerializer serializer = new JavaScriptSerializer(); string[][] list = serializer.Deserialize<string[][]>(json); 
like image 38
Alexander Eroma Avatar answered Sep 18 '22 13:09

Alexander Eroma