2

How can I De-serialize following json?

{
    "data": {
        "11396": {
            "description": "Timer project",
            "status": "ACTIVE",
            "customer": {
                "locations": {},
                "id": 96626
            },
            "tasks": [
                {
                    "description": "Timer Task",
                    "unit": "h",
                    "vatPct": 0.2,
                    "unitPrice": 12,
                    "billable": true,
                    "id": 19660
                }
            ],
            "price": 0,
            "pricing": "UNIT",
            "allowProducts": true,
            "hasUninvoicedItems": false,
            "id": 11396
        },
        "11397": {
            "description": "Timer Project 2",
            "status": "ACTIVE",
            "customer": {
                "locations": {},
                "id": 96626
            },
            "tasks": [
                {
                    "description": "Timer Task2",
                    "unit": "h",
                    "vatPct": 0.05,
                    "unitPrice": 20,
                    "billable": true,
                    "id": 19655
                }
            ],
            "price": 0,
            "pricing": "UNIT",
            "allowProducts": true,
            "hasUninvoicedItems": false,
            "id": 11397
        }
    },
    "ok": true
}

The problem is that values 11396, 11397 as class name (if convert to c#) which are actually ids of that particular record. so when converting this json to c# using http://json2csharp.com. it shows as this

public class Locations
{
}

public class Customer
{
    public Locations locations { get; set; }
    public int id { get; set; }
}

public class Task
{
    public string description { get; set; }
    public string unit { get; set; }
    public double vatPct { get; set; }
    public double unitPrice { get; set; }
    public bool billable { get; set; }
    public int id { get; set; }
}

public class __invalid_type__11397
{
    public string description { get; set; }
    public string status { get; set; }
    public Customer customer { get; set; }
    public List<Task> tasks { get; set; }
    public double price { get; set; }
    public string pricing { get; set; }
    public bool allowProducts { get; set; }
    public bool hasUninvoicedItems { get; set; }
    public int id { get; set; }
}

public class Locations2
{
}

public class Customer2
{
    public Locations2 locations { get; set; }
    public int id { get; set; }
}

public class Task2
{
    public string description { get; set; }
    public string unit { get; set; }
    public double vatPct { get; set; }
    public double unitPrice { get; set; }
    public bool billable { get; set; }
    public int id { get; set; }
}

public class __invalid_type__11396
{
    public string description { get; set; }
    public string status { get; set; }
    public Customer2 customer { get; set; }
    public List<Task2> tasks { get; set; }
    public double price { get; set; }
    public string pricing { get; set; }
    public bool allowProducts { get; set; }
    public bool hasUninvoicedItems { get; set; }
    public int id { get; set; }
}

public class Data
{
    public __invalid_type__11397 __invalid_name__11397 { get; set; }
    public __invalid_type__11396 __invalid_name__11396 { get; set; }
}

public class RootObject
{
    public Data data { get; set; }
    public bool ok { get; set; }
}

any help is much appreciated.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Jagath Murali
  • 515
  • 3
  • 13
  • 1
    Hope this http://stackoverflow.com/questions/19707810/converting-json-to-c-sharp-classes-in-asp-net-mvc?rq=1 will help you.. – Dineshkumar Dec 11 '13 at 07:19

1 Answers1

3

I resolved this issue by parsing the json string to JTOKEN and the querying the required data. This was possible because my datas inside json was static

JToken token = JObject.Parse(response);
        var justDaily = token["data"];
        ProjectList = new List<Project>();
        foreach (JToken child in justDaily.Children())
        {
            foreach (JToken grandChild in child)
            {
                Project temp = JsonConvert.DeserializeObject<Project>(grandChild.ToString().Replace("\r\n", ""));

                    ProjectList.Add(temp);
            }

        }

Hope this will help someone else also

Jagath Murali
  • 515
  • 3
  • 13