2

I have this kind of json structure:

 { "Root": { "data": [ { "CardName": "card1", "functions": [ { "State": "OPEN", "State": "INHERENT" } ] }, { "CardName": "card2", "functions": [ { "State": "CLOSED", "State": "INHERENT" } ] } ] } } 

And my C# classes are :

 [DataContract] public class Card { [DataMember(Name = "CardName")] public string CardName { get; set; } [DataMember(Name = "functions")] public List<Function> Functions { get; set; } } [DataContract] public class Function { [DataMember(Name = "State")] public string State { get; set; } } 

I would like to parse that structure in order to get a list of cards, and each card containing a list of functions.

At this moment i am trying this:

 string content = string.Empty; using (StreamReader sr = new StreamReader("json")) { string line; while ((line = sr.ReadLine()) != null) { content += line; } } List<Card> dynObj = JsonConvert.DeserializeObject<Card>(content); 

but i only get a list of nulls. Can you please tell me where the problem is ?

5
  • 2
    You could try the other way around too: create a Card, serialise it to JSON and see what the output is? Commented Oct 8, 2013 at 10:21
  • Why are you using dynamic? - The result is on a known type (Article), therefore you can write Article or var. Commented Oct 8, 2013 at 10:26
  • Your definition of the Article class is missing above. Commented Oct 8, 2013 at 10:27
  • ok, indeed that should be Card. i will modify it now. And, i would expect a list of cards, is that possible ? Any method available ? I didn't find one. Commented Oct 8, 2013 at 10:28
  • basically you are missing root object Commented Oct 8, 2013 at 10:31

5 Answers 5

8

By pasting the JSON in Visual Studio (Edit > Paste Special -> Paste JSON as Classes), it tells me the classes for your data should look like this.

public class Rootobject { public Root Root { get; set; } } public class Root { public Datum[] data { get; set; } } public class Datum { public string CardName { get; set; } public Function[] functions { get; set; } } public class Function { public string State { get; set; } } 
Sign up to request clarification or add additional context in comments.

Comments

2

from http://json2csharp.com/

public class Function { public string State { get; set; } } public class Datum { public string CardName { get; set; } public List<Function> functions { get; set; } } public class Root { public List<Datum> data { get; set; } } public class RootObject { public Root Root { get; set; } } 

Comments

1

please changes your jsona as below and try

 "Root": { "data": [{ "CardName": "card1", "functions": [{ "State": "OPEN" }, { "State": "INHERENT" }] }, { "CardName": "card2", "functions": [{ "State": "CLOSED" }, { "State": "INHERENT" }] }] } 

Comments

1

Assuming the missing class is:

public class Article { public List<Card> Root { get; set; } } 

The JSON has a mistake in it, it should be:

{ "Root": [ { "CardName": "card1", "functions": [ { "State": "OPEN", }, { "State": "INHERENT" } ] }, { "CardName": "card2", "functions": [ { "State": "CLOSED" }, { "State": "INHERENT" } ] } ] 

Notice that the functions should each be in a {} and the root class should contain a list of cards.

Alternatively, you could deserialize as List and then skip the wrapping "Root".

Comments

1

There were many issues with the code. I have fixed those in below code:

using System; using System.Collections.Generic; using System.Data; using System.Data.Objects; using System.Data.SqlClient; using System.Globalization; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Runtime.Serialization.Json; using System.Threading; using System.Xml; using ConsoleDemo.Controller; using ConsoleDemo.Model; using Microsoft.Practices.Unity; namespace ConsoleDemo { class Program { static void Main(string[] args) { var data = @"{""Root"": {""data"": [{""CardName"": ""card1"",""functions"": [{""State"": ""OPEN""},{""State"": ""INHERENT""}]},{""CardName"": ""card2"",""functions"": [{""State"": ""CLOSED""},{""State"": ""INHERENT""}]}]}"; RootClass dynObj = JsonHelper.JsonDeserialize<RootClass>(data); //Get the object Console.ReadKey(); } } [DataContract] public class RootClass { [DataMember(Name = "Root")] public Data Root { get; set; } } [DataContract] public class Data { [DataMember(Name = "data")] public List<Card> data { get; set; } } [DataContract] public class Card { [DataMember(Name = "CardName")] public string CardName { get; set; } [DataMember(Name = "functions")] public List<Function> Functions { get; set; } } [DataContract] public class Function { [DataMember(Name = "State")] public string State { get; set; } } public class JsonHelper { /// <summary> /// JSON Serialization /// </summary> public static string JsonSerializer<T>(T t) { var ser = new DataContractJsonSerializer(typeof(T)); var ms = new MemoryStream(); ser.WriteObject(ms, t); var jsonString = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return jsonString; } /// <summary> /// JSON Deserialization /// </summary> public static T JsonDeserialize<T>(string jsonString) { var ser = new DataContractJsonSerializer(typeof(T)); var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); var obj = (T)ser.ReadObject(ms); return obj; } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.