0

I have the following string that I want to convert into object City. This doesn't work but throws:

Could not resolve type: global::Newtonsoft.Json.JsonConvert.DeserializeObject

Jsonstring:

{"result":{"id_city":"XX","city_name":"XXXX","city_province":"BW","city_country":"DE","city_zipcode":null,"city_gps_lat":"XXXXXX","city_gps_lng":"XXXXXX","city_gps_geohash":"XXXXXX","city_image":"XXXXXX","distance":11111}}

City class:

 public class City
 {
      [JsonProperty("id_city")]
      public string id_city { get; set; }
      [JsonProperty("city_name")]
      public string city_name { get; set; }
      [JsonProperty("city_province")]
      public string city_province { get; set; }
      [JsonProperty("city_country")]
      public string city_country { get; set; }
      [JsonProperty("city_zipcode")]
      public string city_zipcode { get; set; }
      [JsonProperty("city_gps_lat")]
      public string city_gps_lat { get; set; }
      [JsonProperty("city_gps_lng")]
      public string city_gps_lng { get; set; }
      [JsonProperty("city_gps_geohash")]
      public string city_gps_geohash { get; set; }
      [JsonProperty("city_image")]
      public string city_image { get; set; }
      [JsonProperty("distance")]
      public string distance { get; set; }
}

Method call:

City stadt = JsonConvert.DeserializeObject<City>(Jsonstring);

installed Newtonsoft.Json 8.0.3 over NuGet

Axel
  • 3,331
  • 11
  • 35
  • 58
Notcor
  • 47
  • 1
  • 8
  • 2
    Have you added a reference to the Json.Net dlls? – Liam Jan 03 '17 at 10:14
  • 1
    Possible duplicate of [How to install JSON.NET using NuGet?](http://stackoverflow.com/questions/4444903/how-to-install-json-net-using-nuget) – Liam Jan 03 '17 at 10:15
  • @Liam yes i added Newtonsoft.Json and also using it – Notcor Jan 03 '17 at 10:16
  • That seems unlikely.... – Liam Jan 03 '17 at 10:17
  • i added the version 8.0.3 – Notcor Jan 03 '17 at 10:18
  • One of your assemblies does not have the correct reference. Check them all and ensure everything that needs to access Json.Net has the assembly reference added. It'll be whichever one is erroring. This is pretty basic stuff.. – Liam Jan 03 '17 at 10:21
  • @Liam i checked all References and they exist this message also only comes in Debug mode, after executing this code the object is still empty but it didnt break the app – Notcor Jan 03 '17 at 10:25
  • @Liam Could be the problem the "result" in front of my json string? – Notcor Jan 03 '17 at 10:37
  • 1
    the `json` doesn't correspond to the type you have created. You'll need another wrapper like -`public class RootObject { public City result { get; set; } }` – Amit Kumar Ghosh Jan 03 '17 at 10:43

1 Answers1

3

Your string represents a class with a result property of City type.

public class YourResultClass
{
    public City result { get; set; }
}

You can deserialize the string to YourResultClass.

YourResultClass stadt = JsonConvert.DeserializeObject<YourResultClass>(Jsonstring);

You can use JSonEditorOnline to check what your json string represents.

HebeleHododo
  • 3,620
  • 1
  • 29
  • 38