How to Deserialize JSON data? How to Deserialize JSON data? json json

How to Deserialize JSON data?


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.


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 streamvar 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);


Step 1: Go to json.org to find the JSON library for whatever technology you're using to call this web service. Download and link to that library.

Step 2: Let's say you're using Java. You would use JSONArray like this:

JSONArray myArray=new JSONArray(queryResponse);for (int i=0;i<myArray.length;i++){    JSONArray myInteriorArray=myArray.getJSONArray(i);    if (i==0) {        //this is the first one and is special because it holds the name of the query.    }else{        //do your stuff        String stateCode=myInteriorArray.getString(0);        String stateName=myInteriorArray.getString(1);    }}