0

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/...
1
  • 1
    Did the deserialization work? Then the question has nothing to do with json and is in fact about how to show the List<Layer> in a DataGrid? In this case, please specify the UI fraework you use (WPF, WinForms, ASP.NET). Commented Oct 27, 2019 at 11:40

1 Answer 1

1

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): enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick! Thank you! I'm creating a Windows form application in .NET.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.