用Newtonsoft.Json反序列化

时间:2016-08-17 22:53:18

标签: c# json rest testing

我是一名学习C#和REST服务的测试人员,对我来说都是新手...... 我有像这样的JSON响应

{ "results" : [
    {
        "address_components" : [
        {
            "long_name" : "1600",
            "short_name" : "1600",
            "types" : [ "street_number" ]
        },
        {
            "long_name" : "Amphitheatre Parkway",
            "short_name" : "Amphitheatre Pkwy",
            "types" : [ "route" ]
        },
        {
            "long_name" : "Mountain View",
            "short_name" : "Mountain View",
            "types" : [ "locality", "political" ]
        },
        {
            "long_name" : "Santa Clara County",
            "short_name" : "Santa Clara County",
            "types" : [ "administrative_area_level_2", "political" ]
        },
        {
            "long_name" : "California",
            "short_name" : "CA",
            "types" : [ "administrative_area_level_1", "political" ]
        },
        {
            "long_name" : "United States",
            "short_name" : "US",
            "types" : [ "country", "political" ]
        },
        {
            "long_name" : "94043",
            "short_name" : "94043",
            "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
     "geometry" : {
        "location" : {
           "lat" : 37.4223434,
           "lng" : -122.0843689
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 37.42369238029149,
              "lng" : -122.0830199197085
           },
           "southwest" : {
              "lat" : 37.42099441970849,
              "lng" : -122.0857178802915
           }
        }
     },
     "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
     "types" : [ "street_address" ]
  }
],
"status" : "OK"
} 

并将其反序列化为对象,只需要" lat"和" lng"在几何/位置

我的代码看起来像

JObject myMap = JObject.Parse(response.Content);
IList<JToken> mylist = myMap["results"][0]["geometry"].Children().ToList();`

 IList<location> spots = new List<location>();
foreach (JToken spot in mylist)
{
    Console.WriteLine("\nthe spots are :\t:"+ spot.ToString());
    location somesopt = JsonConvert.DeserializeObject<location>(spot.ToString());
    Console.WriteLine("\n the lat is :\t" + somesopt.lat.ToString());
    Console.WriteLine("\n the lat is :\t" + somesopt.lng.ToString());
}

我的对象是.........

   public class location
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class geopost
{
    public location geometry;
}

问题总是我在json deseserializobject上得到JsonSerializationException我尝试改变定义mylist的许多排列 但是写不出来......我做错了什么..?

为&#34; Long_name&#34;使用相同的代码结构时和&#34; short_name&#34;在地址组件它工作正常。我知道地址组件是数组,几何/位置是对象。但你怎么处理呢?

2 个答案:

答案 0 :(得分:2)

我和DavidG一样,但是我通过将JSON复制到剪贴板然后在VS上生成剪辑我转到menúEdit - &gt; Paste Special - &gt; {{ 1}}。

生成deseralize所需的类:

Paste JSON as Classes

同样的反序列化方法:

public class Rootobject
{
    public Result[] results { get; set; }
    public string status { get; set; }
}

public class Result
{
    public Address_Components[] address_components { get; set; }
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public string place_id { get; set; }
    public string[] types { get; set; }
}

(...)

答案 1 :(得分:1)

为什么不反序列化到合适的类结构?像这样:

public class AddressComponent
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public List<string> types { get; set; }
}

public class Location
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Northeast
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Southwest
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Viewport
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

public class Geometry
{
    public Location location { get; set; }
    public string location_type { get; set; }
    public Viewport viewport { get; set; }
}

public class Result
{
    public List<AddressComponent> address_components { get; set; }
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public string place_id { get; set; }
    public List<string> types { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
    public string status { get; set; }
}

然后像这样反序列化:

var data = JsonConvert.DeserializeObject<RootObject>(json);

现在您可以直接在对象层次结构上操作:

foreach (var result in data.results)
{
    var lat = result.geometry.location.lat;
}

PS我从json2csharp.com

生成了类层次结构