2013-12-11 27 views
1

如何反序列化下面的json?如何反序列化類名爲動態值的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 
} 

問題是值11396,11397作爲類名稱(如果轉換爲C#),實際上是該特定記錄的ID。所以當轉換這個JSON到C#使用http://json2csharp.com。它顯示爲

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; } 
} 

任何幫助,非常感謝。

+1

希望這http://stackoverflow.com/questions/19707810/converting-json-to-c- sharp-classes-in-asp-net-mvc?rq = 1會幫助你.. – Dinesh

回答

1

我解決了這個問題,通過解析JTO字符串JTOKEN和查詢所需的數據。 這是可能的,因爲我裏面的json DATAS是靜態的

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); 
      } 

     } 

希望這會幫助別人也