2016-08-09 3 views
0

ジオコードAPIのGoogleを使用しています。JSON文字列をすべての日付で取得できますが、オブジェクトで変換します仕事、それは何かを返す、私はJSON.NETを使用して、私は何か間違っている?JSON.NETを使用してオブジェクトにJSON文字列(ジオコードAPIのgoogle)を逆シリアル化する#

すべてのJSONデータで、formatted_addressを使用します。

JSON要求:http://maps.googleapis.com/maps/api/geocode/json?address=av.paulista&sensor=false%22

申し訳ありません私の悪い英語

私のメインフォーム:JSONデータを取得(作業)

private void btnConsumir_Click(object sender, EventArgs e) 
    { 
     string address = txtAddress.Text ; 

     string searchCode = "http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false"; 
     var JSONdata = ""; 


     var httpWebRequest = (HttpWebRequest)WebRequest.Create(searchCode); 
     httpWebRequest.ContentType = "text/json"; 
     httpWebRequest.Method = "POST"; 


     var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()); 

     var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 

     using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) 
     { 
      JSONdata = streamReader.ReadToEnd(); 
     } 

     lblJSON.Text = JSONdata;//its a label 

ここで私は、JSONのformatted_address情報を取得したい:

[...] 
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) 
     { 
      JSONdata = streamReader.ReadToEnd(); 
     } 

     lblJSON.Text = JSONdata;//its a label 

     AddressObject addressObject = JsonConvert.DeserializeObject<addressObject>(JSONdata); 
     string result = addressObject.formatted_address; 

     lblJSON.Text = result; 

これは私のクラスオブジェクトです:

class AddressObject 
    { 
     public string formatted_address { get; set; } 




    } 

ありがとうございました!

答えて

1

apiから戻ってきた結果は、formatted_address以上です。グラフ全体をデシリアライズして、必要なものを抜き出す必要があります。以下のクラス構造で

、あなたがこれを行うことができます:

Rootobject mapdata = JsonConvert.DeserializeObject<Rootobject>(JSONdata); 

...

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

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

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

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

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

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

public class Address_Components 
{ 
    public string long_name { get; set; } 
    public string short_name { get; set; } 
    public string[] types { get; set; } 
} 
+0

ありがとうそんなに! –

関連する問題