2017-04-24 125 views
-1

對於C#和Visual Studio的新手來說,忍耐着我,建議做更好的事情都非常開放。在DataGridView中顯示覆雜的JSON

我想製作一個程序來使用DataGridView顯示和編輯JSON。應用程序將從網上下載一些JSON(獲得該部分),然後將其顯示在應用程序中進行簡單編輯,並在完成編輯後將其上載回服務器。問題是我在JSON顯示的部分卡住了(可能是一個重要的部分)。

我發現我應該使用DataGridView來顯示它(至少這是我從一些搜索得到的印象),我能夠成功下載和反序列化JSON(使用JSON。 NET)使用一些簡單的列表和字典。我希望能夠將JSON反序列化爲類,以便我能夠更輕鬆地導航它,但使用我的嵌套字典和數組證明它非常困難。這裏是一個示例(真正的文件有點長,但我已經剪掉了,所以你得到了要點)。

{ 
    "regular" : [ 
    [ 
     { 
     "description" : "Free E-Booklet", 
     "information" : "http://freeebooknotascam.com", 
     "contentType" : "banner", 
     "message" : "Free E-Booklet (NOT A SCAM!)", 
     "color" : "#811cca" 
     } 
    ], 
    [ 
     { 
     "description" : "Free Money!", 
     "information" : "http://freemoney.com", 
     "title" : "", 
     "contentType" : "web", 
     "thumbnail" : "http://freemoney.com/money" 
     } 
    ] 
    ], 
    "special" : [ 
    { 
     "description" : "Hats", 
     "information" : "", 
     "title" : "", 
     "contentType" : "web", 
     "thumbnail" : "http://hats.com/mlady" 
    }, 
    { 
     "description" : "Signup", 
     "information" : "", 
     "title" : "", 
     "contentType" : "web", 
     "thumbnail" : "http://google.com/signup/thumbnail.png" 
    } 
    ] 
} 

,因爲他們出現,但JSON.NET並不想反序列化,並將其顯示到DataGridView的我已經試過簡單的類只用嵌套的列表和字典。

這是我目前使用的反序列化,它的工作原理,但它不顯示在DataGridView(只顯示計數,我不能遍歷上下)。 JsonConvert.DeserializeObject<Result>(json);結果就是這樣。

public class Result 
{ 
    public List<Dictionary<String, String>> featured; 
    public List<List<Dictionary<String, String>>> regular; 
} 

在一個視圖我想列出的項目的標籤description來回regular字典例如

- Regular 
    - Row One 
    - Free E-Booklet 
    - Row Two 
    - Free Money 

,並在其他將列出special字典

- Special 
    - Hats 
    - Signup 
+1

你有什麼期望在'DataGridView'顯示?它可以爲你顯示一個列表,所以你需要使用LINQ將結果形成一個合適的列表。 –

+0

@Reza Aghaei我想展示一個簡單的列表,可以將其拖放到其他鍵/值中,但是現在只需要獲得一個列出的字典數組並且列表中的描述鍵顯示就沒問題。 –

+1

最好在你的問題中包含所需輸出的例子。此外,似乎您可以創建一個類而不是'Dictionary ',因爲所有元素都具有相同的關鍵字 –

回答

1

我用了JavaScriptSerializer,但它並不重要。

由於字典中的密鑰是不同的並且事先未知(如我所建議的),因此請使用DataTable。它允許您動態創建列。

var text = File.ReadAllText("file.txt"); 
var jss = new JavaScriptSerializer(); 
var root = (Dictionary<string, object>)jss.DeserializeObject(text); 

var data = root.SelectMany(pair => ((object[])pair.Value)); 

var dt = new DataTable(); 
dt.Columns.Add("special", typeof(bool)).ReadOnly = true; 

// regular 
foreach (var arr in data.OfType<object[]>()) 
    foreach (Dictionary<string, object> dict in arr) 
     foreach (var key in dict.Keys) 
      if (!dt.Columns.Contains(key)) 
       dt.Columns.Add(key); 

foreach (var arr in data.OfType<object[]>()) 
    foreach (Dictionary<string, object> dict in arr) 
    { 
     var row = dt.NewRow(); 
     foreach (var pair in dict) 
      row[dt.Columns[pair.Key]] = pair.Value; 
     dt.Rows.Add(row); 
    } 

// special 
foreach (var dict in data.OfType<Dictionary<string, object>>()) 
    foreach (var key in dict.Keys) 
     if (!dt.Columns.Contains(key)) 
      dt.Columns.Add(key); 

foreach (var dict in data.OfType<Dictionary<string, object>>()) 
{ 
    var row = dt.NewRow(); 
    row["special"] = true; 
    foreach (var pair in dict) 
     row[dt.Columns[pair.Key]] = pair.Value; 
    dt.Rows.Add(row); 
} 

dataGridView.DataSource = dt; 

爲了表明哪一組 - 常規或特殊 - 行屬於我,我添加了一個布爾列。相反,您可以使用兩種不同的DataGridView

結果:

enter image description here

+0

之上添加了所需輸出的粗略輪廓非常感謝!完美的作品。對於任何人在將來閱讀這篇文章,你必須添加'使用System.Web.Script.Serialization;'和'使用System.Linq;'以便'JavaScriptSerializer'和'SelectMany'分別工作。 –