使用JsonConvert.DeserializeObject反序列化Json [英] Using JsonConvert.DeserializeObject to deserialize Json

查看:5563
本文介绍了使用JsonConvert.DeserializeObject反序列化Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要转换为对象City的字符串.这不起作用,但是会抛出:

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

无法解析类型:global :: Newtonsoft.Json.JsonConvert.DeserializeObject

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

Jsonstring:

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}}

城市阶层:

 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; }
}

方法调用:

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

通过NuGet安装了Newtonsoft.Json 8.0.3

推荐答案

您的字符串表示具有City类型的result属性的类.

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

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

您可以将字符串反序列化为YourResultClass.

You can deserialize the string to YourResultClass.

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

您可以使用 JSonEditorOnline 检查您的json字符串代表什么.

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

这篇关于使用JsonConvert.DeserializeObject反序列化Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆