2011-11-09 2 views
0

저는 JSON에 조금 익숙하여 이것을하기위한 최선의 방법을 이해하려고 노력하고 있습니다. postcode와 energyrating이라는 두 가지 변수가 JSON에 넣고 for 루프로 파싱하려고합니다.루프 동안이 올바른 JSON 구조는 무엇입니까?

하나의 변수에서 작동하도록 만들 수 있지만 두 개가 있으면 작동하지 않습니다.

function addNew(postcodes) { 
if(postcodes.length > 0) { 
    for(var i = 0; i < postcodes.length; i++) { 

     var address = postcodes[i]; 
     var rating = energyrating[i]; 

     geocoder.geocode({ 'address': address }, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       map.setCenter(results[0].geometry.location); 

       var image = '../img/markers/' + rating + '.png'; 
       var marker = new google.maps.Marker({ 
        map: map, 
        position: results[0].geometry.location, 
        icon: image 
       }); 
      } else { 
       alert("Geocode was not successful for the following reason: " + status); 
      } 
     }); 
    } 
} else { 
    alert("Sorry, no data was found."); 
} 
} 

가 어떻게이 두 변수와 함께 작동하도록받을 수 있나요 :

여기
header('Content-type: application/json'); 

$postcodeArray = array('postcodes' => array("E6 2JG","SE1 2AQ","DA1 1DZ"), 'energyrating' => array("A","B","C","D","E","F","G")); 

die(json_encode($postcodeArray)); 

내 jQuery를 수 있습니다 : 여기

내 JSON입니까?

+0

'addNew()'함수는 어떻게 불려지나요? 또한 'postcodes'와 'energyrating'이라는 데이터가 다른 길이의 배열이므로 동일한 for 루프를 사용하여 두 배열을 모두 루프 할 수 있습니까? – nnnnnn

답변

0

정확히 작동하지 않는 것은 무엇입니까?

코드를 읽으면 코드에 오류가 있다고 말할 수 있습니다. energyrating 인수는 addNew 함수에 누락되었습니다

function addNew(postcodes, energyrating) { 

이 같은 함수를 호출한다고 가정 : 그런 데이터를 저장하는

addNew(jsonData.postcodes, jsonData.energyrating); 
0

사용 JSON 변수 :

$postcodeArray = '{"postcodes":{"0":E6, "1":"2JG", "3":"SE1 2AQ","4":"DA1 1DZ"}, "energyrating":{"0":"A","1":"B","2":"C","3":"D","4":"E","5":"F","6":"G"}}'; 

의 자리에
$postcodeArray = JSON.parse('postcodes' => array("E6 2JG","SE1 2AQ","DA1 1DZ"), 'energyrating' => array("A","B","C","D","E","F","G")); 

값에 액세스 할 수 있습니다.

또는 addNew 첫 줄에 json_decode(postcodes)을 입력하십시오.

관련 문제