2012-06-01 3 views
1

jQuery와 구문 분석하려는 2MB JSON 개체가 있습니다. 전체 개체를 "timeline.js"라는 파일에 쏟아 부었습니다. 필자는 필요에 따라 레코드를 잡기 위해 데이터 세트를 파싱 해보기를 바라고 있습니다.jQuery로 외부 JSON 파일 구문 분석

내 데이터 세트가 XML 파일로 시작되었지만 jQuery를 사용하여 데이터를 가져 와서 DOM에 배치 할 때 JSON이 더 효율적이라고 읽었습니다.

다음은 내 개체의 첫 번째 레코드입니다. 1016의 'profileid'로 기록을 잡으려면 어떻게이 객체를 파싱합니까? 의

{ 
timeline:{ 
    record:[ 
     { 
      profileid:1016, 
      title:'Adam', 
      parentprofileid:0, 
      type:'Person', 
      minzoomlevel:29, 
      maxzoomlevel:66, 
      isapproxstart:1, 
      isapproxend:1, 
      startdate:-4181, 
      enddate:-3251, 
      shortdescription:'Name means "red" or "man" he is...', 
      article:'<div><span>The first member of...', 
      status:22, 
      scriptures:{ 
       scripture:[ 
        { 
         profileid:1016, 
         scripturetext:'Genesis 2:7', 
         referencetext:'Birth' 
        }, 
        { 
         profileid:1016, 
         scripturetext:'Genesis 5:4', 
         referencetext:'Death' 
        } 
       ] 
      } 
     }, 

답변

5

jQuery를 parseJSON 그러나 잘 작동 (이미 데이터를 수신 한 후 jQuery를 파싱한다) jQuery를 AJAX를 사용 JSON의 데이터 유형을 설정할 때 불필요하다.

그러나 실제로, 귀하의 실제 질문은 프로필 ID가 같은 레코드를 찾는 방법입니다. 1016. 모든 항목이 배열에 있으므로 배열을 반복하고 현재 항목에 대해 설정된 profileId를 확인하는 것이 유일한 방법입니다. 예를 들면 : - 'code`var OBJ jQuery.parseJSON = ('timeline.js ')를

for(var i in items){ 
    if(items[i].profileid == 1016){ 
     //execute whatever you want to do. 
    } 
} 
1

만들기 사용 : jQuery.parseJSON(json)은 - 잘 형성 JSON 문자열을 가져 와서 결과 자바 스크립트 객체를 반환합니다. 예

var obj = jQuery.parseJSON('{"name":"John"}'); 
alert(obj.name === "John"); 
+1

이렇게함으로써,'code'을 -이 저장 메모리의 전체 JSON 객체 않는가? 또는 obj를 호출 할 때마다 JSON을 단순히 구문 분석합니까? – matthoiland