2016-08-30 1 views
-3

ich folgende gültige JSON-Daten haben (getestet von http://jsonlint.com/):Get Teilmenge von Daten aus 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 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    } 
} 

Ich mag würde die JSON-Daten zu einem Datensatz zu übergeben, damit ich es von Gridview-Steuerelement angezeigt werden soll.

Gibt es eine Möglichkeit, um nur Anzeigewerte innerhalb „sample2“ gefunden, wo ich folgendes anzeigen kann:

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

Ich verwende JSON.NET der JsonConvert den Wert aus einem Datensatz zu Gridview-Steuerelement zu übergeben

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

aber einen Fehler zu erhalten:

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

bin ich einige fehlt Ding? Bitte beraten. Vielen Dank.

Antwort

1

Zuerst, Sie benötigen eine gültige class für Ihre JSON zu analysieren. So etwas wie diese (Dank 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; } 
} 

Weiter Ihre JSON.net Aufruf ist falsch, es sollte wie folgt aussehen:

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 

Auch Ihre Frage:

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

ist eine einfache NO. Sie müssen Ihr JSON-Modell neu strukturieren oder das RootObject zu einem geeigneten ViewModel analysieren.

2

Sie müssen keine Klassen für die Daten erstellen, die Sie nicht benötigen, können Sie das Teil zugreifen, die Sie direkt benötigen:

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

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

Klassen benötigt:

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

Dann Sie können einfach data an Ihren Gürtel binden.

+0

@BWA Es ist ein lokaler Variablenname, der das json-Dokument enthält, aktualisiert. – user3185569