2017-03-21 13 views
0

私はjsonオブジェクトをループしようとしていますが、それが得られないようです。以下はjsonオブジェクトです。私はループしていくつかの詳細を取得したい別のサイトから来るjsonオブジェクトです。jsonオブジェクト、配列をループする

{ 
    "results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "Jai Nagar Road", 
       "short_name" : "Jai Nagar Road", 
       "types" : [ "route" ] 
      }, 
      { 
       "long_name" : "Soorpur", 
       "short_name" : "Soorpur", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "Chittaurgarh", 
       "short_name" : "Chittaurgarh", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "Rajasthan", 
       "short_name" : "RJ", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "India", 
       "short_name" : "IN", 
       "types" : [ "country", "political" ] 
      }, 
      { 
       "long_name" : "312023", 
       "short_name" : "312023", 
       "types" : [ "postal_code" ] 
      } 
     ], 
     "formatted_address" : "Jai Nagar Road, Soorpur, Rajasthan 312023, India", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 25.0047284, 
        "lng" : 75.0057195 
       }, 
       "southwest" : { 
        "lat" : 25.0006706, 
        "lng" : 75.0015796 
       } 
      }, 
      "location" : { 
       "lat" : 25.0026729, 
       "lng" : 75.00372419999999 
      }, 
      "location_type" : "GEOMETRIC_CENTER", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 25.0047284, 
        "lng" : 75.0057195 
       }, 
       "southwest" : { 
        "lat" : 25.0006706, 
        "lng" : 75.0015796 
       } 
      } 
     }, 
     "place_id" : "ChIJZW2OMHBTbzkRRo_IZ8-KtOc", 
     "types" : [ "route" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "Chainsingh Ji Ka Rajpura", 
       "short_name" : "Chainsingh Ji Ka Rajpura", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "Chittaurgarh", 
       "short_name" : "Chittaurgarh", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "Rajasthan", 
       "short_name" : "RJ", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "India", 
       "short_name" : "IN", 
       "types" : [ "country", "political" ] 
      }, 
      { 
       "long_name" : "312023", 
       "short_name" : "312023", 
       "types" : [ "postal_code" ] 
      } 
     ], 
     "formatted_address" : "Chainsingh Ji Ka Rajpura, Rajasthan 312023, India", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 25.0045601, 
        "lng" : 75.01064 
       }, 
       "southwest" : { 
        "lat" : 24.98435, 
        "lng" : 74.99471989999999 
       } 
      }, 
      "location" : { 
       "lat" : 24.9913835, 
       "lng" : 74.99977579999999 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 25.0045601, 
        "lng" : 75.01064 
       }, 
       "southwest" : { 
        "lat" : 24.98435, 
        "lng" : 74.99471989999999 
       } 
      } 
     }, 
     "place_id" : "ChIJf8czVWNTbzkR8ZR9tJK11fY", 
     "types" : [ "locality", "political" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "312023", 
       "short_name" : "312023", 
       "types" : [ "postal_code" ] 
      }, 
      { 
       "long_name" : "India", 
       "short_name" : "IN", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "312023, India", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 25.134308, 
        "lng" : 75.2009999 
       }, 
       "southwest" : { 
        "lat" : 24.8438019, 
        "lng" : 74.84139999999999 
       } 
      }, 
      "location" : { 
       "lat" : 25.0362956, 
       "lng" : 75.0143824 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 25.134308, 
        "lng" : 75.2009999 
       }, 
       "southwest" : { 
        "lat" : 24.8438019, 
        "lng" : 74.84139999999999 
       } 
      } 
     }, 
     "place_id" : "ChIJc0fixnhUbzkRWCXjs3L0pNw", 
     "types" : [ "postal_code" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "Chittaurgarh", 
       "short_name" : "Chittaurgarh", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "Rajasthan", 
       "short_name" : "RJ", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "India", 
       "short_name" : "IN", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "Chittaurgarh, Rajasthan, India", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 25.2151799, 
        "lng" : 75.81283000000001 
       }, 
       "southwest" : { 
        "lat" : 24.20684, 
        "lng" : 74.10463 
       } 
      }, 
      "location" : { 
       "lat" : 24.9560515, 
       "lng" : 74.6868815 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 25.2151799, 
        "lng" : 75.81283000000001 
       }, 
       "southwest" : { 
        "lat" : 24.20684, 
        "lng" : 74.10463 
       } 
      } 
     }, 
     "place_id" : "ChIJmbw1Yl6gaDkRHM-dp2uZAa4", 
     "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "Rajasthan", 
       "short_name" : "RJ", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "India", 
       "short_name" : "IN", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "Rajasthan, India", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 30.1932842, 
        "lng" : 78.26338109999999 
       }, 
       "southwest" : { 
        "lat" : 23.0632669, 
        "lng" : 69.484413 
       } 
      }, 
      "location" : { 
       "lat" : 27.0238036, 
       "lng" : 74.21793260000001 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 30.1932842, 
        "lng" : 78.26338109999999 
       }, 
       "southwest" : { 
        "lat" : 23.0632669, 
        "lng" : 69.484413 
       } 
      } 
     }, 
     "place_id" : "ChIJNw7j9_o-ajkR5YjABkWbK7U", 
     "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "India", 
       "short_name" : "IN", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "India", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 35.5087008, 
        "lng" : 97.39535869999999 
       }, 
       "southwest" : { 
        "lat" : 6.4626999, 
        "lng" : 68.1097 
       } 
      }, 
      "location" : { 
       "lat" : 20.593684, 
       "lng" : 78.96288 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 35.5087008, 
        "lng" : 97.39498069999999 
       }, 
       "southwest" : { 
        "lat" : 6.7535159, 
        "lng" : 68.16288519999999 
       } 
      } 
     }, 
     "place_id" : "ChIJkbeSa_BfYzARphNChaFPjNc", 
     "types" : [ "country", "political" ] 
     } 
    ], 
    "status" : "OK" 
} 

これをループする可能性がある場合はお知らせください。私の問題は私のウェブサイトの訪問者を追跡したいです。私は訪問者の正確な場所が欲しい。 ありがとうございます。

+0

出力配列はどのようになりますか? – siddiq

+0

こんにちはSiddiq、別のページからjsonとして来て、私はちょうどループし、上記のフォーマットされたアドレスを取得する応答です。応答はこの>>>>から来ている "http://maps.googleapis.com/maps/api/geocode/json?latlng=74,62" – Francisco

答えて

-1
// Yes you can apply the loop. First of all you need to decode JSON to an array. 
$resultArray = json_decode($str,true); 

foreach($resultArray as $row){ 
..... 

} 
+0

こんにちはムハンマド、迅速な応答に感謝します。私の問題は1ページ目です。私はユーザーの座標を取得しています。次に、取得メソッドを使用して別のページ(Google)にユーザーの座標を送信しています。応答がjsonになると、この時点で、しかし私は知らない何らかの理由で私は傾ける。私はそれを変換することを望んで私のPHPページに送ったが、私はそれを長い道のりを取っていると思う。上記のjsonオブジェクトをPHPに直接送信せずにループすることができれば助言してください。以下は私のページでやっていた混乱です。 – Francisco

+0

$(document).ready(function(){ $ .get( "http://maps.googleapis.com/maps/api/geocode/json?latlng=74,62"、function(data1){ var rsp = data1; $ .post( "jsontophp.php"、{rsp:rsp}、関数(データ){ $( "#応答")。テキスト(データ); }); }); } ); – Francisco

+0

jqueryからjsonをループしたい場合は、http://stackoverflow.com/questions/20772417/how-to-loop-through-json-array-in-jqueryのようにすることができます。それが動作していない場合は、あなたは直面している問題は? – siddiq

関連する問題