Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to deserialized nested jason?

Need to convert jason to csv.

[{"Parameter":[{"Name":"Truck speed","Parameter_Source_Address":"","Value":"0"},{"Name":"HP Feedback - Alt

In","Parameter_Source_Address":"","Value":"7"}],"Snapshot_DateTimestamp":"2016-10-11T14:34:09.000Z"},{"Parameter":

[{"Name":"Truck speed","Parameter_Source_Address":"","Value":"0"},{"Name":"HP Feedback - Alt

In","Parameter_Source_Address":"","Value":"7"}],"Snapshot_DateTimestamp":"2016-10-11T14:34:10.000Z"},{"Parameter":

[{"Name":"Truck speed","Parameter_Source_Address":"","Value":"0"},{"Name":"HP Feedback - Alt

In","Parameter_Source_Address":"","Value":"7"}],"Snapshot_DateTimestamp":"2016-10-11T14:34:11.000Z"},{"Parameter":

[{"Name":"Truck speed","Parameter_Source_Address":"","Value":"0"},{"Name":"HP Feedback - Alt

In","Parameter_Source_Address":"","Value":"7"}],"Snapshot_DateTimestamp":"2016-10-11T14:34:12.000Z"}]


classes

public class Parameter
{

public string Name { get; set; }

public string Parameter_Source_Address { get; set; }

public string Value { get; set; }

}

public class Snapshot
{

public List<parameter> Parameter { get; set; }

public string Snapshot_DateTimestamp { get; set; }

}

What I have tried:

var data2 = JsonConvert.DeserializeObject<list<snapshot>>(json);

Then converted list to datatable and datatable to csv.

getting output:
Parameter,Snapshot_DateTimestamp
System.Collections.Generic.List`1[Json2Csv.Parameter],2016-10-11T14:34:09.000Z
System.Collections.Generic.List`1[Json2Csv.Parameter],2016-10-11T14:34:10.000Z
System.Collections.Generic.List`1[Json2Csv.Parameter],2016-10-11T14:34:11.000Z
System.Collections.Generic.List`1[Json2Csv.Parameter],2016-10-11T14:34:12.000Z
Posted
Updated 8-Nov-16 6:48am
v2

1 solution

Use the newtonsoft json library. It's available in Nuget. Lots of people using it, so a google search for specific issues will yield a plethora of possible solutions.

BTW, this very same question WITH THE VERY SAME DATA came up a week or so ago. After a cursory search, this appears to be a repost of an earlier question.
 
Share this answer
 
v4
Comments
peoria123 8-Nov-16 14:14pm    
Yes this is the repost..I am using newtonsoft jason library but not able to deserialized objects of json file.
#realJSOP 8-Nov-16 17:08pm    
Looking at your data, you won't be able to serialize it to csv format without doing some stuff manually in order to flatten the hierarchy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900