How to Deserialize JSON data?

102,747

Solution 1

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.

Solution 2

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);
Share:
102,747
eitan barazani
Author by

eitan barazani

Sr. .Software Engineer with over 20 years of experience, highlights include: • Mobile Software Developer, published the succesfull: HealthOrganizer (Win8 / WP7.1), myNewborn (Win8), ourBaby (Win8), myCamera (Win8 / WP8), myHealth (WP8) &amp; myTip (WP8), myEatingOut(WP8), myMoney(WP8.1), myBills(WP8.1) myLocation (WP8) GPS Tools (WP8) and Car Lease (WP8.1) applications for the Windows 8 and Windows Phone application stores. For full links to the Microsoft stores, please go to: http://mobile.ytd.com/ • Developed an interface to Bluetooth LE devices (Jawbone Bands) using Microsoft UWP APIs. • Part of a team porting iOS and Android Secure Email Client to Windows Phone 8. This application is designed for enterprises users in mind. Using of the Microsoft Exchange ActiveSync protocol. • Won Microsoft’s Windows 8 Hackathon for the Health Organizer application, and won third place for the myTip application in the Microsoft’s APPortunity for the month of April • Used UWP / WinRT / .NET 4.5 to develop Windows 8/8.1/10 applications and Window Phone 7/7.5/8/8.1/10. • Used extensive Data Binding, and MVVM design pattern, Live SDK, Linq, and Background Tasks. • Used REST API to communicate with data sources servers. • Used Scrum / JIRA project management and issue tracking tools, and familiar with Agile software development methodologies. • Used GIT (and SourceTree) for source control. • Worked with Telerik / Syncfusion / ComponentArt / Microsoft WP Toolkit and Callisto tools to accelerate project progress • Used .NET 4.0 to develop WP7 applications to track family medical records. Used Silverlight 3 &amp; 4 for Windows Phone, Data Binding, and MVVM design pattern • Used C# .NET 3.5 to develop a distributed control system for Jetline Engineering. The application was programmed in real time and multithreading • Highly proficient with Object Oriented Programming (C#, C++) techniques including development of classes, custom controls • Participated in all stages of software development including conceptual design, requirements gathering, specification development, hardware and software development, testing and integration http://mobile.ytd.com/

Updated on January 09, 2020

Comments

  • eitan barazani
    eitan barazani over 4 years

    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.