Question

Current I have a project where I'm getting the following sample data ( I want to retrieve only the ids within this json string and stuff them into IEnumerables (explained below):

{
  "states": [
    {
      "id": "AL",
      "text": "Alabama (AL)"
    },
    {
      "id": "CO",
      "text": "Colorado (CO)"
    }
  ],
  "cities": [
    {
      "id": 71761,
      "text": "New Brockton, AL"
    },
    {
      "id": 74988,
      "text": "Nathrop, CO"
    }
  ],
  "zipCodes": []
}

Notice in the zipCodes, I am getting an empty set, so there is no "id" or "text".

I want to be able to create several IEnumerables from the properties found in this JSON string.

I created an object called Locations that looks like this:

public class Location
{
    public IEnumerable<string> States { get; set; }
    public IEnumerable<string> ZipCodes { get; set; }
    public IEnumerable<decimal> Cities { get; set; }
}

The best way I found to going about this approach is to do each data property one by one and convert, formValues is the json string:

JArray arrStates = (JArray)formValues["states"];
JArray arrCities = (JArray)formValues["cities"];
JArray arrZip = (JArray)formValues["zipCodes"];

and then set the properties in the location object as so:

Location loc = new Location();
loc.States = arrStates.Children().Select(m=>m["id"].Value<string>());
loc.ZipCodes = arrCities.Children().Select(m=>m["id"].Value<string>());
loc.Cities = arrZip.Children().Select(m=>m["id"].Value<string>());

I was wondering if there's a better way of doing this instead of doing all this code maintenance for whenever my json response adds a new property. In fact, I think there's going to be about ten more properties added to the json string.

I want it to be reduced down to where I could just update the Location object, and have the json automatically map to the properties that way. Or atleast a solution that has less maintenance than what I'm doing now.

Also I was wondering if JsonConvert.DeserializeObject would work in my case; but read that JSON.NET treats an IEnumerable as an array, so I'm stumped on this one.

Was it helpful?

Solution

JsonConvert.DeserializeObject would work in your case and it will have less maintenance than what you're doing now.

If you enter your json data to http://json2csharp.com, below is the generated class definition that you can use, I renamed RootObject to Location

public class State
{
    public string id { get; set; }
    public string text { get; set; }
}

public class City
{
    public int id { get; set; }
    public string text { get; set; }
}

public class Location
{
    public List<State> states { get; set; }
    public List<City> cities { get; set; }
    public List<object> zipCodes { get; set; }
}

This is how you deserialize the json data into Location

string jsonData = ...; // set the json data here

var location = JsonConvert.DeserializeObject<Location>(jsonData);

You can enumerate through the nested properties to get the ids, for example location.states[0].id will return "AL" and location.cities[1].id will return 74988.

If there's a new property in the json data, let's say it's named countries with id and text like in states, you can create a new Country class

public class Country
{
    public string id { get; set; }
    public string text { get; set; }
}

and add countries property to Location class

public class Location
{
    public List<State> states { get; set; }
    public List<City> cities { get; set; }
    public List<object> zipCodes { get; set; }
    public List<Country> countries { get; set; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top