2017-02-06 19 views
0

JSON數據:如何在C#Windows應用程序中將所需的json數據移除並綁定到datagridview?

{ 
"template": { 
    "section": "Total", 
    "totalRecievedTillDate": null, 
    "receivedFiles": "0", 
    "attendedFiles": "0", 
    "pendingFiles": "0", 
    "totalActionPendingTillDate": null, 
    "totalActionCompletedTappal": null, 
    "last15DaysActionCompleted": null, 
    "last15DaysRecievedTappal": null 
}, 
"sectiontappals": [ 
    { 
     "section": "Collectorate-A Section", 
     "totalRecievedTillDate": null, 
     "receivedFiles": "0", 
     "attendedFiles": "0", 
     "pendingFiles": "0", 
     "totalActionPendingTillDate": null, 
     "totalActionCompletedTappal": null, 
     "last15DaysActionCompleted": null, 
     "last15DaysRecievedTappal": null 
    }, 
    { 
     "section": "Collectorate-B Section", 
     "totalRecievedTillDate": null, 
     "receivedFiles": "0", 
     "attendedFiles": "0", 
     "pendingFiles": "0", 
     "totalActionPendingTillDate": null, 
     "totalActionCompletedTappal": null, 
     "last15DaysActionCompleted": null, 
     "last15DaysRecievedTappal": null 

我要刪除第一次提出專利(模板),並希望在C#從sectiontappals數據綁定到datagridview的。可能嗎?

我嘗試這樣做:

using (var client = new HttpClient()) { 
    string path = "uri"; 
    using (var response = await client.GetAsync(path)) { 
     if (response.IsSuccessStatusCode) { 
      var data = await response.Content.ReadAsStringAsync(); 
      dashbordgrd.DataSource = JsonConvert.DeserializeObject<List<listclass>>(data); 
     } 
    } 
} 
+0

你能分享類listclass的結構嗎? –

+0

我越來越高於json by url @Chetan –

回答

0

下面創建類,只爲我創建了一個模板類只有一個屬性例子的緣故。

public class template 
    { 
     public string section{get; set;} 
     private List<sectiontappal> _sectionappal; 
     public List<sectiontappal> sectiontappals 
     { 
      get 
      { 
       if (_sectionappal == null) 
        _sectionappal = new List<sectiontappal>(); 
       return _sectionappal; 
      } 
      set 
      { 
       _sectionappal = value; 
      } 
     } 
    } 

添加NewtonJson到下面的代碼項目使用&實現你問:

System.IO.StreamReader reader = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "/1.txt"); 
string result = reader.ReadToEnd(); 
var data = JsonConvert.DeserializeObject<template>(result); 
dataGridView1.DataSource = data.sectiontappals; 

我放在你的JSON文件與名稱的1.txt。

謝謝

+0

實際上Json數據來自URL。我可以將URL放在「」/1.txt「的位置嗎? –

+0

使用WebClient類,它在System.Net命名空間中可用 使用(WebClient wc = new WebClient()) var json = wc.DownloadString( 「url」); //解析結果 } 使用此鏈接:http://stackoverflow.com/questions/5566942/how-to-get-a-json-string-from-url – Brijesh

+0

真棒.. 。Chetan Sir ..工作。謝謝你。 –

相關問題