반응형
jQuery에서 JSON 어레이를 루프하는 방법
JSON에서 응답을 받을 수 있는 PHP 페이지가 있습니다.
[{'com':'something'},{'com':'some other thing'}]
루프를 돌려서 각각을 div에 붙이고 싶어요.
내가 시도한 건 다음과 같다.
var obj = jQuery.parseJSON(response);
$.each(obj.com, function(key,value) {
alert(key+':'+value);
}
이 경고는 다음과 같습니다.undefined응답은 JSON 어레이이기도 합니다.
어레이에 개체를 저장하는 기본 키(0,1)가 있습니다.{'com':'some thing'}용도:
var obj = jQuery.parseJSON(response);
$.each(obj, function(key,value) {
alert(value.com);
});
이것을 시험해 보세요.
var data = jQuery.parseJSON(response);
$.each(data, function(key, item)
{
console.log(item.com);
});
또는
var data = $.parseJSON(response);
$(data).each(function(i,val)
{
$.each(val,function(key,val)
{
console.log(key + " : " + val);
});
});
반복하고 있습니다.undefined값, 즉com어레이 오브젝트 속성. 어레이 자체를 반복해야 합니다.
$.each(obj, function(key,value) {
// here `value` refers to the objects
});
또한 jQuery는 전송된 JSON을 지능적으로 해석하려고 하므로 응답을 해석할 필요가 없습니다.사용하시는 경우$.ajax(), 를 설정할 수 있습니다.dataType로.jsonjQuery가 JSON을 해석하는 것을 알 수 있습니다.
그래도 작동하지 않으면 브라우저 콘솔에서 문제 해결을 확인하십시오.
var data = [
{"Id": 10004, "PageName": "club"},
{"Id": 10040, "PageName": "qaz"},
{"Id": 10059, "PageName": "jjjjjjj"}
];
$.each(data, function(i, item) {
alert(data[i].PageName);
});
$.each(data, function(i, item) {
alert(item.PageName);
});
또는 이 방법을 시도해 보십시오.
var data = jQuery.parseJSON(response);
$.each(data, function(key,value) {
alert(value.Id); //It will shows the Id values
});
var data=[{'com':'something'},{'com':'some other thing'}];
$.each(data, function() {
$.each(this, function(key, val){
alert(val);//here data
alert (key); //here key
});
});
키 값 쌍은 다음과 같이 얻을 수 있습니다.
<pre>
function test(){
var data=[{'com':'something'},{'com':'some other thing'}];
$.each(data, function(key,value) {
alert(key);
alert(value.com);
});
}
</pre>
이것을 시험해 보세요.
for(var i = 0; i < data.length; i++){
console.log(data[i].com)
}
이거 먹어봐
var events = [];
alert(doc);
var obj = jQuery.parseJSON(doc);
$.each(obj, function (key, value) {
alert(value.title);
});
언급URL : https://stackoverflow.com/questions/20772417/how-to-loop-through-json-array-in-jquery
반응형
'programing' 카테고리의 다른 글
| JSON이 매우 간단하고 다루기 쉬운데 왜 XML(SOAP)을 사용하는가? (0) | 2023.03.10 |
|---|---|
| Json과 XML을 모두 REST 인터페이스에 출력으로 제공하려는 이유는 무엇입니까? (0) | 2023.03.10 |
| 쿼리 후크 반응 Apollo 조건부 호출 사용 (0) | 2023.03.10 |
| ESLint 파괴 상태 할당을 사용해야 합니다. (0) | 2023.03.10 |
| Oracle에서 인덱스 사용 강제 (0) | 2023.03.10 |