2016-10-06 4 views
0

私はGoogle places apiからJSONを逆シリアル化しようとしています。私のカスタムクラスは以下のように設定され、私のコードはこのようになります。私のプログラムは実行中にエラーをスローしませんが、私のplacesオブジェクトはnullです。JSONをC#オブジェクトリストにデシリアライズ

class PlacesDictionary 
{ 

    public void placesDictionary() 
    { } 

    public Places GetPlaces() 
    { 
     Places places = new Places(); 

     string apiKey = "I have an apiKey"; 
     string googleUrl; 
     googleUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=43.038902,-87.906474&radius=500&type=restaurant&name=cruise&key=" + apiKey; 

     WebRequest request = WebRequest.Create(googleUrl); 
     request.Method = "GET"; 
     request.ContentType = "application/x-www-form-urlencoded"; 
     WebResponse response = request.GetResponse(); 
     Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
     Stream dataStream = response.GetResponseStream(); 
     StreamReader reader = new StreamReader(dataStream); 
     string responseFromServer = reader.ReadToEnd(); 
     places = JsonConvert.DeserializeObject<Places>(responseFromServer); 

     Console.WriteLine(responseFromServer); 
     Console.ReadLine(); 
     reader.Close(); 
     dataStream.Close(); 
     response.Close(); 

     return places; 
    } 
} 

public class Places 
{ 
    public List<Place> places { get; set; } 

    public class Place 
    { 
     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 int price_level { 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 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; } 
     } 
    } 
} 
+0

あなたのレストランのオブジェクトがありますか? –

+0

残念なことに場所にレストランがあります –

+0

質問にJSONを追加しないと、答えはちょうど推測に過ぎません。 – Gusman

答えて

1

は私があなたの代わりに

places = JsonConvert.DeserializeObject<Places>(responseFromServer); 

List<Place> places = JsonConvert.DeserializeObject<List<Place>>(responseFromServer); 

を使用する必要がありますし、ライン以下

Places places = new Places(); 

編集を削除し忘れてはいけないと思う:全回答

 static void Main(string[] args) 
    { 
      string apiKey = "your api key"; 
      string googleUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=43.038902,-87.906474&radius=500&type=restaurant&name=cruise&key=" + apiKey; 

      WebRequest request = WebRequest.Create(googleUrl); 
      request.Method = "GET"; 
      request.ContentType = "application/x-www-form-urlencoded"; 
      WebResponse response = request.GetResponse(); 
      Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
      Stream dataStream = response.GetResponseStream(); 
      StreamReader reader = new StreamReader(dataStream); 
      string responseFromServer = reader.ReadToEnd(); 
      //StreamWriter wr = new StreamWriter("json.txt"); 
      //wr.WriteLine(responseFromServer); 
      //wr.Flush(); 
      //To see what it is inside json 
      Result results = JsonConvert.DeserializeObject<Result>(responseFromServer); 

      Console.WriteLine(responseFromServer); 
      Console.ReadLine(); 
      reader.Close(); 
      dataStream.Close(); 
      response.Close(); 

     } 
    } 

    public class Result 
    { 
     public List<HTMLAttribution> html_attributions { get; set; } 
     public string next_page_token { get; set; } 
     public List<Place> results { get; set; } 
     public string status { get; set; } 

     //Definations of Classes 
     public class HTMLAttribution { } //I don't what it is. It is empty for your url. 

     public class Place 
     { 
      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 int price_level { 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 class Geometry 
      { 
       public Location location { get; set; } 
       public Viewport viewport { get; set; } 
      } 
      public class Location 
      { 
       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 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 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; } 
      } 
     } 
    } 
+0

場所はすでにリストになっているので、クラス設定を変更する必要はありますか? –

+0

Placeクラスを変更しないで、Placeクラスのみを削除します。 –

+0

正に、クラスプレイス全体は必要ではなく、リストプロパティも配置されません。必要なのはプレイスクラスだけです。 – Bearcat9425

0

(私があなただったら)私が何かをしようとするだろう:

Places places = JsonConvert.DeserializeObject(responseFromServer);

によって places = JsonConvert.DeserializeObject(responseFromServer); を交換し、あなたのクラスの場所にデフォルトコンストラクタを追加 Places places = new Places();

を削除PlacesDictionaryで追加したようなものです。

+0

はい、残念な場所は、これが動作するときにレストランを保持します。 –

0

1. Placeクラスのみが必要です。

2.Initialize List<Place> places=new List<Place>()

3.Deserialize使用場所

places = JsonConvert.DeserializeObject<places>(responseFromServer); 
+0

私はpublicクラスの場所クラスを持っています。 places {get;セット; }。私はそれを取り除くことができ、それは自分のクラスではなく、リストにシリアライズすると言っていますか? –

+0

場所= JsonConvert.DeserializeObject (responseFromServer);プレイスがもはやクラスではない場合、タイプとして使用することはできません –

+0

デシリアライズ中にリスト "インスタンス"のインスタンスに対する悪い回答が返されました。はいプレイスはクラスになりません – inan

関連する問題