1
미디어 배열이 비어 있지 않은 경우에만 JSON의 행을 읽고 싶습니다.미디어 배열이 비어 있지 않은 경우에만 JSON의 행 읽기
그러나 아래 코드는 작동하지 않습니다. 제가
- (data.rows [J] .media.length> = 1) 또는
- (data.rows [J] .media! = 미정)
정의했지만 윌 EMPTY 인 미디어에 대해서는 여전히 어레이를 읽습니다. .
// JSON data
{
author: "Mary",
media: [ ],
},
{
author: "Nelson",
media: [
{
type: "Image",
link: "https://...jpeg"
}
]
}
// Jquery to get the data from JSON
$.ajax('https://rest.xyz.com/', {
type: 'GET',
dataType: 'json',
success: function(data) {
var m = 0;
for(j in data.rows){
if (data.rows[j].media.length >=1) {
if (data.rows[j].media[0].type == 'Video'){ }
else { }
}
}
}
json 객체를 참조 할 때 실수가 아니라면 length가 작동합니다. 'data.rows [j] .media.length> = 1' 대신'data.rows [j] .media [0]! = null'을 시도하십시오. –
당신이 제안한 것처럼 위의 두 가지 방법을 시도했지만 TypeError가 있습니다. 정의되지 않은 개체 (평가 'data.rows [j] .media [0] .type') – user21