Reputation: 11
I'm trying to get all the name and href values from the json into a data gridview and have gotten stuck. At the moment the gridview remains blank. Any ideas how to fill the gridview?
JSON:
{
"layers": {
"layer": [
{
"name": "tiger:giant_polygon",
"href": "http://localhost:8080/geoserver/rest/layers/tiger%3Agiant_polygon.json"
},
{
"name": "tiger:poi",
"href": "http://localhost:8080/geoserver/rest/layers/tiger%3Apoi.json"
},
{
"name": "tiger:poly_landmarks",
"href": "http://localhost:8080/geoserver/rest/layers/tiger%3Apoly_landmarks.json"
},
{
"name": "tiger:tiger_roads",
"href": "http://localhost:8080/geoserver/rest/layers/tiger%3Atiger_roads.json"
}
]
}
}
Class:
class geoserverLayerName
{
public class Layer
{
public string name { get; set; }
public string href { get; set; }
}
public class Layers
{
public List<Layer> layer { get; set; }
}
public class RootObject
{
public Layers layers { get; set; }
}
}
Deserialize:
private void deserialiseJSON(string strJSON)
{
try
{
var jPerson = JsonConvert.DeserializeObject<Layers>(strJSON);
dgwLayers.DataSource = jPerson.layer;
}
catch(Exception ex)
{
debugOutputJSON("We had a problem: " + ex.Message.ToString());
}
}
This is how I want the gridview to be filled:
Name: Href:
tiger:giant_polygon http://localhost:8080/...
tiger:poi http://localhost:8080/...
Upvotes: 0
Views: 180
Reputation: 1600
Let's start with the fact that your deserialization request is not really correct. What you really need (according to your definition) is:
// That's the root object holding JSON data
var jPerson = JsonConvert.DeserializeObject<RootObject>(strJSON);
// And that's in "layer" you have a list of layers
dgwLayers.DataSource = jPerson.layers.layer;
I suggest you placing breakpoint right over dgwLayers.DataSource = (...); to make sure in your case deserialization really happened (in case you missed to provide some definitions; for exact definitions you gave here on SO deserialization passes with my example).
Depending on exact list of techologies you use for data grid view (WinForms, WPF, WebForms or custom control), you might also need to set correct columns and data properties for you DataGridView, before this the update of UI will take place.
For instance, WinForms (no need to mess with DataGridView columns properties unless you want custom headers, like uppercased naming etc.; definition taken from data source):
Upvotes: 1