JS 프로그래밍과 함께 꽤 n00b이므로 바보 같은 질문 일지 모르지만 검색 시간이 지나면 그물에서 작동하는 해결책을 찾지 못했습니다.배열로 다중 JSon 레코드 읽기
여러 개의 JSON 피드가 있습니다.
http://address/ci/c/1이 제공 :
{ "LGICUS01OperationResponse": { "ca": { "ca_phone_mobile": "07799 123456", "ca_request_id": "01ICUS", "ca_return_code": 0, "ca_dob": "11.07.1950", "ca_last_name": "Pandy", "ca_num_policies": 0, "ca_phone_home": "01962 811234", "ca_email_address": "[email protected]", "ca_house_name": "", "ca_policy_data": "", "ca_customer_num": 1, "ca_first_name": "Andrew", "ca_house_num": "34", "ca_postcode": "PI101OO" } } }
http://address/ci/c/2이 제공 :
{ "LGICUS01OperationResponse": { "ca": { "ca_phone_mobile": "123", "ca_request_id": "01ICUS", "ca_return_code": 0, "ca_dob": "30.09.1965", "ca_last_name": "Tracey", "ca_num_policies": 0, "ca_phone_home": "", "ca_email_address": "[email protected]", "ca_house_name": "Tracey Island", "ca_policy_data": "", "ca_customer_num": 2, "ca_first_name": "Scott", "ca_house_num": "", "ca_postcode": "TB14TV" } } }
서버 (데이터가 가상의 테스트 데이터입니다)이 같은 URL 당 하나의 다층 JSON 기록을주고있다
등등. 이제는 Ajax로 JSON recor를 읽을 수있게되었지만 getCurrentJson 함수 외부에서 해당 정보 배열을 전달하는 데 심각한 문제가 있습니다.
<!DOCTYPE html>
<html>
<head>
<title>JSON test</title>
<script src="jquery.js" type="text/javascript"></script>
<script>
var myjson = [];
for (i = 1; i < 11; i++) {
getCurrentJson(i);
console.log(myjson[i].LGICUS01OperationResponse.ca.ca_phone_mobile);
}
function getCurrentJson(current){
$.ajax({
dataType: "json",
url: "http://192.49.208.193:9081/ci/c/"+current,
success: function(data){
myjson[current] = data;
console.log(myjson[current]);
console.log(myjson[current].LGICUS01OperationResponse.ca.ca_phone_mobile);
}
});
}
</script>
</head>
<body>
</body>
</html>
: 여기
{
"rows":[
{"ca_customer_num":1", "ca_first_name:"Andrew",...}
{"ca_customer_num":2", "ca_first_name:"Scott",...}
...
]
}
코드이다 : 최적의 상황은 먼저 두 개의 파라미터가 다음과 같이 "표준 방법"에 나중에 조작을위한 사용자 정보의 배열이 될 수 있도록 생략 될 것이라는 것
이제 ajax-function 내부의 console.log 출력은 Object 정보와 "ca_phone_mobile"번호를 출력하지만 for 루프의 첫 번째 콘솔 출력은 다음과 같습니다 : "Uncaught TypeError : 정의되지 않은 (...) 속성의 'LGICUS01OperationResponse' ". 일부 toString 데이터 형식 변환 또는 유사합니까? myjson 배열을 getCurrentJson 함수에 전달하려고 시도했지만 성공하지는 못했습니다.
'$ .ajax'는 비동기 호출을 수행합니다. 이것이 for 루프의'console.log'가 예상 결과를 보여주지 못하고 비동기 요청이 아직 완료되지 않은 이유입니다. –