使用newton.json进行json反序列化

时间:2016-08-30 07:46:42

标签: c# windows-phone-8 json.net

当我使用json.net反序列化json时

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Tower A, Spaze iTech Park",
               "short_name" : "Tower A, Spaze iTech Park",
               "types" : [ "premise" ]
            },
            {
               "long_name" : "Sohna - Gurgaon Road",
               "short_name" : "Sohna - Gurgaon Rd",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Block S",
               "short_name" : "Block S",
               "types" : [ "political", "sublocality", "sublocality_level_3" ]
            },
            {
               "long_name" : "Sector 49",
               "short_name" : "Sector 49",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Gurgaon",
               "short_name" : "Gurgaon",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Gurgaon",
               "short_name" : "Gurgaon",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Haryana",
               "short_name" : "HR",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "122018",
               "short_name" : "122018",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Tower A, Spaze iTech Park, Sohna - Gurgaon Rd, Block S, Sector 49, Gurgaon, Haryana 122018, India",
         "geometry" : {
            "location" : {
               "lat" : 28.4138566,
               "lng" : 77.04217849999999
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 28.4152055802915,
                  "lng" : 77.0435274802915
               },
               "southwest" : {
                  "lat" : 28.4125076197085,
                  "lng" : 77.04082951970848
               }
            }
         },
         "place_id" : "ChIJq9P3-JoiDTkRzBq_Q6Ala_A",
         "types" : [ "premise" ]
      },

   ],
   "status" : "OK"
}

后续课程实例中的数据将来自非公开成员而不是结果列表

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 Northeast2
   {
       public double lat { get; set; }
       public double lng { get; set; }
   }

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

   public class Bounds
   {
       public Northeast2 northeast { get; set; }
       public Southwest2 southwest { get; set; }
   }

   public class Geometry
   {
       public Location location { get; set; }
       public string location_type { get; set; }
       public Viewport viewport { get; set; }
       public Bounds bounds { 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 GeoCodingModel
   {
       public List<Result> results { get; set; }
       public string status { get; set; }
   }

我的rootclass是geocodingmodel类 我的rootclass是geocodingmodel类

2 个答案:

答案 0 :(得分:1)

类看起来没问题,反序列化工作正常。哪里有问题?

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

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 Northeast2
   {
       public double lat { get; set; }
       public double lng { get; set; }
   }

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

   public class Bounds
   {
       public Northeast2 northeast { get; set; }
       public Southwest2 southwest { get; set; }
   }

   public class Geometry
   {
       public Location location { get; set; }
       public string location_type { get; set; }
       public Viewport viewport { get; set; }
       public Bounds bounds { 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 GeoCodingModel
   {
       public List<Result> results { get; set; }
       public string status { get; set; }
   }

public class Program
{
    static public void Main()
    {
        string j = "{ \"results\" : [ { \"address_components\" : [ { \"long_name\" : \"Tower A, Spaze iTech Park\", \"short_name\" : \"Tower A, Spaze iTech Park\", \"types\" : [ \"premise\" ] }, { \"long_name\" : \"Sohna - Gurgaon Road\", \"short_name\" : \"Sohna - Gurgaon Rd\", \"types\" : [ \"route\" ] }, { \"long_name\" : \"Block S\", \"short_name\" : \"Block S\", \"types\" : [ \"political\", \"sublocality\", \"sublocality_level_3\" ] }, { \"long_name\" : \"Sector 49\", \"short_name\" : \"Sector 49\", \"types\" : [ \"political\", \"sublocality\", \"sublocality_level_1\" ] }, { \"long_name\" : \"Gurgaon\", \"short_name\" : \"Gurgaon\", \"types\" : [ \"locality\", \"political\" ] }, { \"long_name\" : \"Gurgaon\", \"short_name\" : \"Gurgaon\", \"types\" : [ \"administrative_area_level_2\", \"political\" ] }, { \"long_name\" : \"Haryana\", \"short_name\" : \"HR\", \"types\" : [ \"administrative_area_level_1\", \"political\" ] }, { \"long_name\" : \"India\", \"short_name\" : \"IN\", \"types\" : [ \"country\", \"political\" ] }, { \"long_name\" : \"122018\", \"short_name\" : \"122018\", \"types\" : [ \"postal_code\" ] } ], \"formatted_address\" : \"Tower A, Spaze iTech Park, Sohna - Gurgaon Rd, Block S, Sector 49, Gurgaon, Haryana 122018, India\", \"geometry\" : { \"location\" : { \"lat\" : 28.4138566, \"lng\" : 77.04217849999999 }, \"location_type\" : \"ROOFTOP\", \"viewport\" : { \"northeast\" : { \"lat\" : 28.4152055802915, \"lng\" : 77.0435274802915 }, \"southwest\" : { \"lat\" : 28.4125076197085, \"lng\" : 77.04082951970848 } } }, \"place_id\" : \"ChIJq9P3-JoiDTkRzBq_Q6Ala_A\", \"types\" : [ \"premise\" ] }, ], \"status\" : \"OK\" }";



        GeoCodingModel ro = JsonConvert.DeserializeObject<GeoCodingModel>(j);

        Console.WriteLine(ro.results[0].geometry.location_type);

    }

}

<强>结果

  

ROOFTOP

答案 1 :(得分:0)

尝试使用此链接,您可以了解解析并从JSON字符串

获取valuse

Fiddle