Bart
Bart

Reputation: 432

Deserialize JSON C#

I am having a problem deserializing a json object because it does not consist of an array.

A sample of the json string :

 {
  "Data": {
    "A1": {
      "name": "",
      "code": "",      
      "type": ""
    },
    "A2": {
     "name": "",
      "code": "",      
      "type": ""
    },
    "A3": {
      "name": "",
      "code": "",      
      "type": ""
    }
  }
}

And here is my code, the json string will be read from a file, and cannot be changed.

var json = "{\"Data\":{\"A1\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"},\"A2\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"},\"A3\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"}}}";
var jobject = JsonConvert.DeserializeObject<DataContainer>(json);


public class Data
{
    public string name { get; set; }
    public string code { get; set; }            
    public string type { get; set; }
}

public class DataContainer
{
    public List<Data> Data { get; set; }
}

Only way I have managed to do this is by changing the json to use an array like the sample below, but I would hope to solve this without having to change the format of the json file.

{
  "Data": [
    {
      "name": "",
      "code": "",      
      "type": ""
    },
    {
     "name": "",
      "code": "",      
      "type": ""
    },
    {
      "name": "",
      "code": "",      
      "type": ""
    }
  ]
}

Thanks

Upvotes: 1

Views: 167

Answers (3)

Brian Rogers
Brian Rogers

Reputation: 129807

If you want to use your original JSON data format, change the definition of your DataContainer class to use a Dictionary<string, Data> instead of a List<Data>:

public class DataContainer
{
    public Dictionary<string, Data> Data { get; set; }
}

Upvotes: 0

Mehmet
Mehmet

Reputation: 706

I created 2 classes and manually deserialize the json object like this:

class Program
{
    static void Main(string[] args)
    {
        var json = "{\"Data\":{\"A1\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"},\"A2\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"},\"A3\":{\"name\":\"\",\"code\":\"\",\"type\":\"\"}}}";
        var data = (JsonConvert.DeserializeObject(json) as Newtonsoft.Json.Linq.JObject).First.First;

        List<DataItem> list = new List<DataItem>();
        foreach (var item in data.ToList())
        {
            list.Add(new DataItem()
            {
                Name = ((Newtonsoft.Json.Linq.JProperty)(item)).Name,
                A = JsonConvert.DeserializeObject<A>(item.First.ToString())
            });
        }
    }

    class DataItem
    {
        public string Name { get; set; } //A1, A2, A3 ....
        public A A { get; set; }
    }

    class A
    {
        public string name { get; set; }
        public string code { get; set; }
        public string type { get; set; }
    }
}

hope this help you.

Upvotes: 0

Antonio Pelleriti
Antonio Pelleriti

Reputation: 859

your json contains a Data object which contains three different objects: A1, A2, A3.

So I think you need to implement three different classes, and a container

public class A1
{
    public string name { get; set; }
    public string code { get; set; }
    public string type { get; set; }
}

public class A2
{
    public string name { get; set; }
    public string code { get; set; }
    public string type { get; set; }
}

public class A3
{
    public string name { get; set; }
    public string code { get; set; }
    public string type { get; set; }
}

public class Data
{
    public A1 A1 { get; set; }
    public A2 A2 { get; set; }
    public A3 A3 { get; set; }
}

Upvotes: 1

Related Questions