2014-01-14 58 views
0

我想acheive以下JSON對象從C#代碼JSON對象從C#

{ 
    "Animals": { 
     "name": "Animals", 
     "data": [ 
      [ 
       "Cows", 
       2 
      ], 
      [ 
       "Sheep", 
       3 
      ] 
     ] 
    }, 
    "Fruits": { 
     "name": "Fruits", 
     "data": [ 
      [ 
       "Apples", 
       5 
      ], 
      [ 
       "Oranges", 
       7 
      ], 
      [ 
       "Bananas", 
       2 
      ] 
     ] 
    }, 
    "Cars": { 
     "name": "Cars", 
     "data": [ 
      [ 
       "Toyota", 
       1 
      ], 
      [ 
       "Volkswagen", 
       2 
      ], 
      [ 
       "Opel", 
       5 
      ] 
     ] 
    } 
} 

我試圖json2C#鏈接,它給了我這個階層結構

public class Animals 
{ 
    public string name { get; set; } 
    public List<List<object>> data { get; set; } 
} 

public class Fruits 
{ 
    public string name { get; set; } 
    public List<List<object>> data { get; set; } 
} 

public class Cars 
{ 
    public string name { get; set; } 
    public List<List<object>> data { get; set; } 
} 

public class RootObject 
{ 
    public Animals Animals { get; set; } 
    public Fruits Fruits { get; set; } 
    public Cars Cars { get; set; } 
} 

我的第一個問題是生成的類通過代碼是靜態的(動物,水果,汽車),實際上它可能會越來越少,它可能是一些新的類別,所以每次我需要爲每個類別創建一個新類時?我該如何處理?

第二我如何從這些類填充相同的結構。

請耐心等待,因爲我是初學者級別的程序員。

+0

首先,這不是有效的JSON。如果你試圖達到你在那裏寫的東西,你會遇到問題。你想要更像這樣的東西:http://pastebin.com/2gbQd2jb – brandonscript

+0

我已經更新了這個問題,實際上引號錯過了 – alhashmiya

+0

我看到,但是,它仍然是一個非常複雜的模式,你正在嘗試做什麼。而且,既然你沒有爲這些值定義鍵,他們大多是無意義的。查看我的pastebin示例以獲取更適合的格式。 – brandonscript

回答

2

試試這個。創建新的控制檯應用程序。您將需要JSON.NET庫。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Newtonsoft.Json; 

namespace ConsoleApplication7 
{ 
    class Item : List<object> 
    { 

     public Item() 
     { 
      this.Add(""); // for name; 
      this.Add(0); // for value; 
     } 
     [JsonIgnore] 
     public string Name { get { return this[0].ToString(); } set { this[0] = value; } } 
     [JsonIgnore] 
     public int Value { get { return (int)this[1]; } set { this[1] = value; } } 
    } 

    class Category 
    { 
     public string name { get; set; } 
     public List<Item> data { get; set; } 
     public Category() 
     { 
      this.data = new List<Item>(); 
     } 
    } 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var all = new Dictionary<string, Category> 
       { 
        { 
         "Animals", new Category() 
          { 
           name = "Animals", 
           data = 
            new List<Item>() 
             { 
              new Item() {Name = "Cows", Value = 2}, 
              new Item() {Name = "Sheep", Value = 3} 
             } 
          } 

         //include your other items here 
        } 
       }; 

      Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(all)); 
      Console.ReadLine(); 
     } 
    } 
} 
+0

尊敬的codenoire和Tim S.,Million Thankx你們兩個都幫助我很多,它再次解決了我的問題,再次感謝 – alhashmiya

0

安裝JSON.NET庫。

與創建的類

然後:

string jsonStr = "{'Animals': {name: 'Animals', data: [['Cows', 2], ['Sheep', 3] ] },'Fruits': { name: 'Fruits', data: [['Apples', 5], ['Oranges', 7], ['Bananas', 2] ] }, 'Cars': { name: 'Cars', data: [ ['Toyota', 1], ['Volkswagen', 2], ['Opel', 5] ] } }"; 

RootObject Myobj = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonStr); 
1

你不需要單獨AnimalsFruits等類。他們可以合併。

public class Category 
{ 
    public string name { get; set; } 
    public List<List<object>> data { get; set; } 
} 

而且,由於在根對象中的項目列表中可以改變,你應該使用Dictionary<string, Category>,而不是你已經產生的RootObject類。您的JSON是無效的,(與http://jsonlint.com/測試它),但是這會產生像你的榜樣的第一部分:

var dict = new Dictionary<string, Category> 
{ 
    { "Animals", new Category 
     { 
      name = "Animals", 
      data = new List<List<object>> 
      { 
       new List<object> { "Cows", 2 }, 
       new List<object> { "Sheep", 3 } 
      } 
     } 
    }, 
}; 
string serialized = JsonConvert.SerializeObject(dict, Formatting.Indented); 

產生如下(我使用Json.NET的系列化這裏)。其他類型會相似。 (見Object and Collection Initializers對我使用了列表和字典初始化語法的詳細信息,如果你不熟悉它,因爲他們的Add方法基本上只是快捷方式)

{ 
    "Animals": { 
    "name": "Animals", 
    "data": [ 
     [ 
     "Cows", 
     2 
     ], 
     [ 
     "Sheep", 
     3 
     ] 
    ] 
    } 
} 

如果你有什麼數據類型的選擇應是的,我覺得這是更好地與一類這樣的事情來代替List<object>,更清晰:

public class Item 
{ 
    public string name { get; set; } 
    public int quantity { get; set; } 
}