Json只被反序列化为一个数组对象

时间:2017-05-18 14:00:45

标签: c# json google-api deserialization

我正在尝试阅读Google Nearby Places API,但由于一些奇怪的原因,当json被反序列化时,我的数组只包含一个20的对象。 如果有人对这个谜团有所了解,我们将非常感激。

这是我试图反序列化的json数据:

{
   "html_attributions" : [],
   "next_page_token" : "CqQCEgEAAJaY2BXnovCpj_poiYe4xpYf3MeG7K8jDuWSXA0vLjUaEtJXdx02WMEwha-opqU3bu53Asqk-BNo1nkM3AxJboxNa_tPAgF2MBck2tYqoi4vT2b688K5P8uIc1R-pUK43JfbUiQbHXRbIFxITsqQr7eipqu8LOkmjB0YsUqLQGTk6qEFtAdpaaNpXCmwLOEbDH0zRIQ93Hh5rAixD-qYvUVpLydZirOUGnKttLCzxebsQUEuhRdegEsCTF9eerEKh4UKrcNUuIo-InNzl8eAwxKhfhuA7UbOF_qwEqvAw-0jd9ADvQxqFzGPtKlW8osIgQByRXGHo6my6w1fH7EfFdkyD7m4_7u8Gd8y1J6_FWysYO0jmZc4fORCau4sSMgGfRIQycDKwBteF23rIUfL-p2y6xoUviw5O-NvxBFVNUcyPs3MbTaNJFA",
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : -33.8709434,
               "lng" : 151.1903114
            },
            "viewport" : {
               "northeast" : {
                  "lat" : -33.8694072197085,
                  "lng" : 151.1917756802915
               },
               "southwest" : {
                  "lat" : -33.8721051802915,
                  "lng" : 151.1890777197085
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "e644f7f34cf875b9919c6548f1b721947362850a",
         "name" : "Lunch Cruise with Jazz on Sydney Harbour",
         "opening_hours" : {
            "open_now" : true,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 292,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/105423912060796272053/photos\"\u003eLunch Cruise with Jazz on Sydney Harbour\u003c/a\u003e"
               ],
               "photo_reference" : "CmRYAAAAXZ_EwtUdHz9ygfjeYyDNJ0D_Bw3SNUw-Q3yXdpR-L5EyLGzdUQFOg5Ird7wppV50dCP8b2xtCmktAN5CcNBTYmhdojr3ZlMvNkiZTtLbM4cE9LhT7KHVekhuQX4tiAgfEhDta1eIaptCZfxoR8IJOWkjGhRJ2qeNNdYBXpmtizVlKL-1Ll9n6g",
               "width" : 438
            }
         ],
         "place_id" : "ChIJLfySpTOuEmsRPCRKrzl8ZEY",
         "rating" : 3,
         "reference" : "CmRRAAAAwpe8CREsyzSI_-4MagTJIBrlb-MNxItMrcKWbmXD8TI4FnG7Akby3jUNJBXWTqW7urN0gl7p_uU0C9whyxrUJZWems2fA3IFo_ElO9ATI-kwoQXVQtlGTw04Zkg0yhmkEhB8R-cRUnyVWLd5lTPxrds_GhSyoVq8pEbViMmcVQjF0UaMomneMg",
         "scope" : "GOOGLE",
         "types" : [
            "night_club",
            "bar",
            "restaurant",
            "food",
            "point_of_interest",
            "establishment"
         ],
         "vicinity" : "37 Bank Street, Pyrmont"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.8671207,
               "lng" : 151.2016385
            },
            "viewport" : {
               "northeast" : {
                  "lat" : -33.8657717197085,
                  "lng" : 151.2029874802915
               },
               "southwest" : {
                  "lat" : -33.8684696802915,
                  "lng" : 151.2002895197085
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "c620902a8565dd4c4d605fecbe4f9b585d881b62",
         "name" : "Blue Line Cruises",
         "place_id" : "ChIJvwSIiTiuEmsR8hEazPa4W7U",
         "reference" : "CmRSAAAAz7zMj99x1HjVK2qKp1Iki-tHYIV0Qz5J_PlvZD0cHks2-pD76D9IXBdWt4qnjdLT2wNzXdKqGeVCeS2oQNW4c00mZjew3oj-emo-fn3UoDV2SRniF2sl0bgSH6Q_cQH2EhC-qzspkhfyrzTNzY9QhBEuGhRqu9i_Ry22HiGYvK2uzhNJR35V4Q",
         "scope" : "GOOGLE",
         "types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
         "vicinity" : "Australia"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.867591,
               "lng" : 151.201196
            },
            "viewport" : {
               "northeast" : {
                  "lat" : -33.86624201970849,
                  "lng" : 151.2025449802915
               },
               "southwest" : {
                  "lat" : -33.86893998029149,
                  "lng" : 151.1998470197085
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "a97f9fb468bcd26b68a23072a55af82d4b325e0d",
         "name" : "Australian Cruise Group",
         "opening_hours" : {
            "open_now" : false,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 2448,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/118410684014024830328/photos\"\u003eMichael Mak\u003c/a\u003e"
               ],
               "photo_reference" : "CmRYAAAAEHtRlILy8HdDJAw3oa5jhMA4MIW9J9jYUYto_vCejzRoN9gEiGAq4GBpbAgwhtGPe7CjKX4jyRjrSVzJdbddhYhUj5SPPjqg5bA-hZt9NG1VAsgUktnHZPE3fq5wVw83EhBhf8PPHYin0FQwhKsQChNYGhSinnvfBU_rr7S1ab3z_-uBGrEYgQ",
               "width" : 3264
            }
         ],
         "place_id" : "ChIJrTLr-GyuEmsRBfy61i59si0",
         "rating" : 4.8,
         "reference" : "CmRRAAAAc2nTxLrX_BmwqkhWxKYMNQpRmDLZlU-jgFyQOfTBAeUlnzOla4AU5ZUFL7US-t8NrwmgCIQYjSdvjNdLwsNjHQrrdQdSt8_q5VtGXBgNcQzL8eczkNUfeHcvXToGjTKkEhAyuauhQhfb9X7xmS64EJurGhSLDXCaOdtRBoK0YmavfC1picKF7w",
         "scope" : "GOOGLE",
         "types" : [
            "travel_agency",
            "restaurant",
            "food",
            "point_of_interest",
            "establishment"
         ],
         "vicinity" : "32 The Promenade, King Street Wharf 5, Sydney"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.8677371,
               "lng" : 151.2016935
            },
            "viewport" : {
               "northeast" : {
                  "lat" : -33.8664006697085,
                  "lng" : 151.2029067302915
               },
               "southwest" : {
                  "lat" : -33.8690986302915,
                  "lng" : 151.2002087697085
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "ce4ffe228ab7ad49bb050defe68b3d28cc879c4a",
         "name" : "Sydney Showboats",
         "opening_hours" : {
            "open_now" : false,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 900,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/105231077511067622688/photos\"\u003eChristie and Matt Snapshots of Food\u003c/a\u003e"
               ],
               "photo_reference" : "CmRYAAAAV4f1akIe5Kbh3yAtZziLKGwPXvN9qMG7I4P76OXB1B51lLUb8brun6zm0bD70BFTah6WyMMVOBwf_BYPOGMk3rF92eFRLvJOyYxrYYxxpBM5UVLz14EvENJcoo78GzV1EhAGyejRsDjuub5kyV6IJEHfGhTk_f8dWrvIx6Li_dZv4g79wSw7Xg",
               "width" : 1600
            }
         ],
         "place_id" : "ChIJjRuIiTiuEmsRCHhYnrWiSok",
         "rating" : 4.1,
         "reference" : "CmRSAAAAIUfa-ey-n8JeqIkg-QkqgFMwvWkXGituimgpD0kKC6r81MNFwGJHogWx4vRklKdStm1gZnKoriuugK-_gHK_oe2cmcyjtMvFlm79XVi3qP6UOKk47X6jA7NOaDu1WFedEhDbSLWPrkJw1y-YOKzB8NRPGhQ13Gq-m6OeKd1LDVICr6RC52VJMQ",
         "scope" : "GOOGLE",
         "types" : [
            "travel_agency",
            "restaurant",
            "food",
            "point_of_interest",
            "establishment"
         ],
         "vicinity" : "32 The Promenade, Sydney"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.87104699999999,
               "lng" : 151.189736
            },
            "viewport" : {
               "northeast" : {
                  "lat" : -33.8693729197085,
                  "lng" : 151.1912852302915
               },
               "southwest" : {
                  "lat" : -33.8720708802915,
                  "lng" : 151.1885872697085
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "3458f23c154e574552e0722773a46f384816b241",
         "name" : "Vagabond Cruises",
         "opening_hours" : {
            "open_now" : true,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 1200,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/101516907347198229066/photos\"\u003eVagabond Cruises\u003c/a\u003e"
               ],
               "photo_reference" : "CmRYAAAAUb14jHhngpxuAjTBfcJ0rYhmjD3lLr-sHhS8D72eHFyvqPi5SojeuxhUnNv8WXwxVCr_VCMKyqDCbVi6AGZqpbe5-ojbMNaD37MfJ4m37nA1cifP15Osw39WSSE7TtlAEhCu3TCrA_NPLTY3mCNdLyl2GhTlOgZGvQspx12wQGAHGNh26mEeEw",
               "width" : 1800
            }
         ],
         "place_id" : "ChIJLfySpTOuEmsRMFymbMkVkOE",
         "rating" : 3.7,
         "reference" : "CmRSAAAAfBHUWq20M5YndE6MgHHiuhrzLGIfVhG4b0Y1zrYBJdzoFaKfMiDmF7aL-zfwY-NyLMQFdnzZKp0W2Nujydcy5g85rR3tCb1roR6UN8bmT0G1rRcZbCPTO0FNRK5Bax8fEhBLCo0bITmRBdPKu6lp0MbcGhTMBcikSVn6V30U3YJdeDL46bQY8Q",
         "scope" : "GOOGLE",
         "types" : [
            "travel_agency",
            "restaurant",
            "food",
            "point_of_interest",
            "establishment"
         ],
         "vicinity" : "37 Bank Street, Pyrmont"
      },
      {
         "geometry" : {
            "location" : {
               "lat" : -33.8686058,
               "lng" : 151.2018206
            },
            "viewport" : {
               "northeast" : {
                  "lat" : -33.8673641197085,
                  "lng" : 151.2033695302915
               },
               "southwest" : {
                  "lat" : -33.8700620802915,
                  "lng" : 151.2006715697085
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "21a0b251c9b8392186142c798263e289fe45b4aa",
         "name" : "Rhythmboat Cruises",
         "photos" : [
            {
               "height" : 480,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/104066891898402903288/photos\"\u003eRhythmboat Cruises\u003c/a\u003e"
               ],
               "photo_reference" : "CmRYAAAAQoKfbo8toSJdsHuy9BbYqRJDwbhvurNzkCIj65Dqnr_y14BgeqUP1WfL2fgD2BUWAzP_1fvbmTJdq5QsKF_Xzc1BPxQgBn9naW9eAH0J-LG8xSbnQ_CoXYlUmR1iXwSbEhDCNAse8BSTr_O7ztgDC9-BGhQMgDhAcIDY1TOJJyUXk8NAjLz8UA",
               "width" : 640
            }
         ],
         "place_id" : "ChIJyWEHuEmuEmsRm9hTkapTCrk",
         "rating" : 3.6,
         "reference" : "CmRSAAAAVI_so0hyPbi2dP8iBDnQ9y_dDza42GhU_GZaOPyAFMehYP7QIEdlPAMkw1a1GE_s77uxsdnw7bhKl4djkQbK7dxnWaqvZl7XLr1CnaYQuf1VpH-HUMJW8Mc3dxig9OqVEhDo4LgQM7j87F1xQS2Ew73yGhSMNCUMLE5iL280CbVFZiJbczSWQA",
         "scope" : "GOOGLE",
         "types" : [
            "travel_agency",
            "restaurant",
            "food",
            "point_of_interest",
            "establishment"
         ],
         "vicinity" : "King Street Wharf, King Street, Sydney"
      },        
      {
         "geometry" : {
            "location" : {
               "lat" : -33.86473759999999,
               "lng" : 151.2013113
            },
            "viewport" : {
               "northeast" : {
                  "lat" : -33.8633802697085,
                  "lng" : 151.2028230802915
               },
               "southwest" : {
                  "lat" : -33.8660782302915,
                  "lng" : 151.2001251197085
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
         "id" : "3b915b9bd520e5fcd19b5ece8aa53d67765f2aea",
         "name" : "Banksii",
         "opening_hours" : {
            "open_now" : true,
            "weekday_text" : []
         },
         "photos" : [
            {
               "height" : 2988,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/106372957228079765255/photos\"\u003eMohammad Khan\u003c/a\u003e"
               ],
               "photo_reference" : "CmRYAAAA9WKq8Zl2GF3A35KukQWzLWTMKdkDzgTz7lBEuJLP89KM0qmle62WMnfEX4f4LD2Q8LiwB50DQA-ydVRrjZIKBdZ-LFTdX2maheksT7VkEbNy_yODjhiIEaUiRycuDm3cEhCJTY3QUHiULJMivayKVFboGhT9NMVFT9RD3jGDCRToc8DLDbPH0Q",
               "width" : 5312
            }
         ],
         "place_id" : "ChIJgXD2qEeuEmsR0jnXdMoFBMo",
         "rating" : 4.1,
         "reference" : "CmRSAAAAzwPCIjY0nVJtdEkbZ48282l3UekXlPu2QAnl7MHddh_zykB-HKuodk9hjilgGvBg9sWxxgMWURJKHXg5Hg8vPB2abMhcRgucvpnstsnH0HHwO1imUeDpOixGUqbjHUWiEhA_FyaNqXpXOD_mxhqHqVUoGhTJXMte2rMzk3h3BR9-QS-EMiJmLg",
         "scope" : "GOOGLE",
         "types" : [ "restaurant", "food", "point_of_interest", "establishment" ],
         "vicinity" : "33 Barangaroo Avenue, Barangaroo"
      }
   ],
   "status" : "OK"
}

我们都试图使用json2csharp和其他许多东西,但没有成功。 第一个项目被完美地反序列化,但结果数组中只存储了一个结果。这是我的反序列化类:

class NearbyPlaceAPI
{
    public List<object> html_attributions { get; set; }
    public string next_page_token { get; set; }
    public Result[] results { get; set; }
    public string status { 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 Viewport viewport { get; set; }
}

public class OpeningHours
{
    public bool open_now { get; set; }
    public List<object> weekday_text { get; set; }
}

public class Photo
{
    public int height { get; set; }
    public List<string> html_attributions { get; set; }
    public string photo_reference { get; set; }
    public int width { get; set; }
}

public class Result
{
    public Geometry geometry { get; set; }
    public string icon { get; set; }
    public string id { get; set; }
    public string name { get; set; }
    public OpeningHours opening_hours { get; set; }
    public List<Photo> photos { get; set; }
    public string place_id { get; set; }
    public double rating { get; set; }
    public string reference { get; set; }
    public string scope { get; set; }
    public List<string> types { get; set; }
    public string vicinity { get; set; }
    public int? price_level { get; set; }
}

要反序列化我的json,我有以下代码:

var NearbyPlaceAPIurl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=restaurant&keyword=cruise&key=MYKEY";
var jsondata = new System.Net.WebClient().DownloadString(NearbyPlaceAPIurl);

NearbyPlaceAPI PlaceAPIResult = JsonConvert.DeserializeObject<NearbyPlaceAPI>(jsondata);

对我而言,它看起来都是正确的,并且所有内容都是完美的反序列化,除了它只反序列化其中一个结果对象,最后我的结果数组中有一个对象。

由于

1 个答案:

答案 0 :(得分:0)

你试过吗

public List<Result> results { get; set; }
而不是       public Result[] results { get; set; }