user3138251
user3138251

Reputation: 67

Json Deserialize Exception

I'm trying to run the following code, but get an exception:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.DLL but was not handled in user code

Additional information: Error converting value "country" to type 'LocalGasPrices.Station'. Path '', line 1, position 9.

        var jsonObj = JObject.Parse(URL);
        var results = jsonObj["stations"].Children().Values();
        var details = new List<Station>();

        foreach (JToken result in results)
        {
            var st = result.ToString();
            var searchResult = JsonConvert.DeserializeObject<Station>(st);
            details.Add(searchResult);

        }

        foreach (Station s in details)
        {
            this.txtDebug.Text += s.address;

        }

And here are the classes for the JsonDeserialization:

public class Status
{
    public string error { get; set; }
    public int code { get; set; }
    public string description { get; set; }
    public string message { get; set; }
}

public class GeoLocation
{
    public string city_id { get; set; }
    public string city_long { get; set; }
    public string region_short { get; set; }
    public string region_long { get; set; }
    public string country_long { get; set; }
    public string country_id { get; set; }
    public string region_id { get; set; }
}

public class Station
{
    public string country { get; set; }
    public string price { get; set; }
    public string address { get; set; }
    public string diesel { get; set; }
    public string id { get; set; }
    public string lat { get; set; }
    public string lng { get; set; }
    public string station { get; set; }
    public string region { get; set; }
    public string city { get; set; }
    public string date { get; set; }
    public string distance { get; set; }
}

public class RootObject
{
    public Status status { get; set; }
    public GeoLocation geoLocation { get; set; }
    public List<Station> stations { get; set; }
}

And here is an example of the JSON response:

 {
    "status": {
        "error": "NO",
        "code": 200,
        "description": "none",
        "message": "Request ok"
    },
    "geoLocation": {
        "city_id": "147",
        "city_long": "Saint-Laurent",
        "region_short": "QC",
        "region_long": "Quebec",
        "country_long": "Canada",
        "country_id": "43",
        "region_id": "35"
    },
    "stations": [
        {
            "country": "Canada",
            "price": "3.65",
            "address": "3885, Boulevard Saint-Rose",
            "diesel": "0",
            "id": "33862",
            "lat": "45.492367",
            "lng": "-73.710915",
            "station": "Shell",
            "region": "Quebec",
            "city": "Saint-Laurent",
            "date": "3 hours agp",
            "distance": "1.9km"
        }
    ]
}

Upvotes: 2

Views: 2178

Answers (5)

Edwin van Vliet
Edwin van Vliet

Reputation: 567

Try the following.

First create a RootObject

public class RootObject
{
public RootObject(){}
public Status status {get;set;}
public GeoLocation GeoLocation {get;set;}
public List<Stations> Stations {get;set;}
}

public class Status
{
public Status(){}

[JsonProperty("error")]
public string Error {get;set;}
//all properties
}

public class GeoLocation
{
public GeoLocation{get;set;}

[JsonProperty("city_id")]
public int CityId {get;set;}
//all properties
}

public class Station
{
public Station(){}
// all properties just like previous classes
}

Then:

var result = serializer.DeserializeObject<RootObject>(jsonInput);

this should give you the RootObject with a status, GeoLocation and list of stations.

Upvotes: 0

Std_Net
Std_Net

Reputation: 1096

try something like this:

public class JsonData
{
  [JsonProperty("status")]
  public JObject Status{get;set;}

  [JsonProperty("geoLocation")]
  public JObject Location{get;set;}


  [JsonProperty("stations")]
  public List<JObject> Stations{get;set;}
}

public class FinalData
{
  public Status StatusField{get;set;}
  public GeoLocation LocationField{get;set;}
  public List<Station> StationsList{get;set;}
}

}

then

JsonSerializer serializer = new JsonSerializer();
JsonReader reader = new JsonTextReader(new StringReader(your_json));
var jObj = serializer.Deserialize<JsonData>(reader);
var result = new FinalData();
result.StatusField = new Status{ message = jObj.Status["message"].ToString();};
result.LocationField = new GeoLocation{jObj.location["city_id"].ToString();};
result.StationsList = new List<Station>();
foreach(var row = jObj.Stations)
{
  result.StationsList.Add(new Station{country = row["country"].ToString(), address = row["address"].ToString()};
}

Upvotes: 0

anthony7060
anthony7060

Reputation: 36

I believe you don't need .Values() in results, .Values() selected the properties of the station.

var results = jsonObj["stations"].Children();

Upvotes: 2

Ulugbek Umirov
Ulugbek Umirov

Reputation: 12807

Use the following:

var details = jsonObj["stations"].Select(t => t.ToObject<Station>()).ToList();

Upvotes: 1

Amit Bhatiya
Amit Bhatiya

Reputation: 2621

change Your class name as "stations" which you get in JSON.

Upvotes: 0

Related Questions