2016-08-30 16 views
-3

から一部のデータを取得:は、私は(<a href="http://jsonlint.com/" rel="nofollow">http://jsonlint.com/</a>からテスト)以下の有効なJSONデータを持っているJSON

{ 
    "alpha": { 
    "one": 1, 
    "two": "2" 
    }, 
    "bravo": { 
    "sample1": { 
     "one": "1", 
     "two": "2", 
     "three": 3 
    }, 
    "sample2": [ 
     { 
     "id": 123, 
     "content": "alpha", 
     "photos": [ 
      { 
      "caption": "photo1", 
      "location": [ 
       { 
       "url": "http://website.com/abc.jpg", 
       "width": 800, 
       "height": 600 
       }, 
       { 
       "url": "http://website.com/def.jpg", 
       "width": 800, 
       "height": 600 
       } 
      ] 
      }, 
      { 
      "caption": "photo2", 
      "location": [ 
       { 
       "url": "http://website.com/ghi.jpg", 
       "width": 800, 
       "height": 600 
       }, 
       { 
       "url": "http://website.com/jkl.jpg", 
       "width": 800, 
       "height": 600 
       } 
      ] 
      } 
     ] 
     }, 
     { 
     "id": 456, 
     "content": "bravo", 
     "photos": [ 
      { 
      "caption": "photo3", 
      "location": [ 
       { 
       "url": "http://website.com/mno.jpg", 
       "width": 800, 
       "height": 600 
       }, 
       { 
       "url": "http://website.com/pqr.jpg", 
       "width": 800, 
       "height": 600 
       } 
      ] 
      }, 
      { 
      "caption": "photo4", 
      "location": [ 
       { 
       "url": "http://website.com/stu.jpg", 
       "width": 800, 
       "height": 600 
       }, 
       { 
       "url": "http://website.com/vwx.jpg", 
       "width": 800, 
       "height": 600 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    } 
} 

私はGridViewコントロールからそれを表示できるようにDataSetにJSONデータを渡すしたいと思います。私は、GridViewコントロールにデータセットから値を渡すJSON.NETのJsonConvertを使用してい

id  content  caption  url 
123 alpha  photo1  http://website.com/abc.jpg 
456 bravo  photo3  http://website.com/mno.jpg 

私は次のように表示できる「サンプル2」の中に発見のみ表示値に方法はあります

DataSet ds = new DataSet(); 
ds = JsonConvert.DeserializeObject<DataSet>("JSON file here"); 
GridView.DataBind(); 

が、エラーを受信:

Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path 'alpha'...

私はいくつかをしないのですもの?お知らせ下さい。ありがとうございました。

答えて

1

最初にとなるようにするには、JSONの構文解析に有効なclassが必要です。このような何か(json2csharpのおかげで):

public class Alpha 
{ 
    public int one { get; set; } 
    public string two { get; set; } 
} 

public class Sample1 
{ 
    public string one { get; set; } 
    public string two { get; set; } 
    public int three { get; set; } 
} 

public class Location 
{ 
    public string url { get; set; } 
    public int width { get; set; } 
    public int height { get; set; } 
} 

public class Photo 
{ 
    public string caption { get; set; } 
    public List<Location> location { get; set; } 
} 

public class Sample2 
{ 
    public int id { get; set; } 
    public string content { get; set; } 
    public List<Photo> photos { get; set; } 
} 

public class Bravo 
{ 
    public Sample1 sample1 { get; set; } 
    public List<Sample2> sample2 { get; set; } 
} 

public class RootObject 
{ 
    public Alpha alpha { get; set; } 
    public Bravo bravo { get; set; } 
} 

あなたJSON.netコールは、それは次のようになり、間違っている:

DataSet ds = new DataSet(); 
var root = JsonConvert.DeserializeObject<RootObject>("JSON String here"); // not the file! 
// Insert here some magic to convert your RootObject to DataSet 

またあなたの質問:

Is there a way to only display values found inside "sample2" where I can display the following

は簡単ですいいえ。 JSONモデルを再構成するか、RootObjectを適切なViewModelに解析する必要があります。

2

あなたは、あなたが直接必要な部分にアクセスすることができ、必要のないデータのためのクラスを作成する必要はありません。必要な

JObject jsonTree = JObject.Parse(json); 
var sample2 = jsonTree["bravo"]["sample2"].ToString(); 

List<Sample2> data = JsonConvert.DeserializeObject<Sample2>(sample2); 

クラス:

public class Photo 
{ 
    public string caption { get; set; } 
    public List<Location> location { get; set; } 
} 

public class Sample2 
{ 
    public int id { get; set; } 
    public string content { get; set; } 
    public List<Photo> photos { get; set; } 
} 

を次にdataをあなたのgirdに簡単に束縛することができます。

+0

@BWA jsonドキュメントを保持するローカル変数名です。更新されました。 – user3185569

関連する問題