2012-11-02 3 views
0

데이터 객체가 있고 내부에 json 데이터가있는 데이터 객체가 있습니다. 다음은 어떻게 생겼는지입니다. 파일의 이름은 여기jsonery for for 루프를 사용하여 json 데이터를 읽을 수 없습니다.

var data = 
[ 
{ 
    title: 'this is title one', 
    commit: 'this is commit' 
}, 
{ 
    title: 'this is title two', 
    commit: 'this is commit' 
}, 
{ 
    title: 'this is title three', 
    commit: 'this is commit' 
}, 
{ 
    title: 'this is title four', 
    commit: 'this is commit' 
}, 
{ 
    title: 'this is title five', 
    commit: 'this is commit' 
}, 
{ 
    title: 'this is title six', 
    commit: 'this is commit' 
}, 
{ 
    title: 'this is title seven', 
    commit: 'this is commit' 
} 
] 

data.js 내가 오류 위

Cannot read property 'title' of undefined 

이 줄에 내 콘솔에서이 오류를 얻고있다 이제 내 코드

for (var i = 0; i < data.length; i++) { 
       $(container).load(view, function() { 
        console.log(data[i].title); 
        li.append('<img src="../images/profile.png" alt="Profile photo" />'); 
        li.append('<span class="commit">' + data[i].title + '</span>'); 
        li.append('<p><a href="#" class="comment">' + data.commit + '</a></p>'); 
        li.append('<section class="clear"></section>'); 
        $('#home ul').append(li); 
       }); 
      } 

입니다

li.append('<span class="commit">' + data[i].title + '</span>'); 

이것을 고치는 방법?

답변

0

음, data.js는 실제로 json이 아닙니다. 해서 getJSON에 부하로부터

제 1 스위치()()

$.getJSON(view, function(data) { }) 

옆 라인

var data = 

를 제거 그냥 JSON 내용이있다. jQuery는 JSON을 인수 데이터에 할당합니다.

0

json 배열에 오류가 있습니다.

당신은 당신의 JSON 유효성을 검사 할 수 있습니다

여기 http://json.parser.online.fr/

그래서 여야합니다

{ 
"title": ["this is title one", "this is title two", "this is title three", "this is title four", "this is title five", "this is title six", "this is title seven" ] 
, 
"commit": [ "this is commit", "this is commit", "this is commit", "this is commit", "this is commit", "this is commit", "this is commit" ] 
} 
관련 문제