2016-07-17 2 views
0

jQuery를 사용하여 JSON 파일의 데이터를 구문 분석하고 표시하고 있습니다. 브라우저에서 침을 뱉어 낼 수있는 모든 것이 있지만 JSON 파일의 키 중 하나에 값 배열이 있습니다. 여기 내 jQuery 코드입니다jQuery를 사용하여 배열로 JSON 객체를 표시하는 방법

{ 
    "category" : "Stuff", 
    "location_id" : { 
    "longitude" : "-71.89237903999964", 
    "latitude" : "41.6809076720005", 
    "human_address" : "{\"address\":\"156 Plainfield Pike Rd\",\"city\":\"Plainfield\",\"state\":\"CT\",\"zip\":\"06374\"}" 
    }, 
} 

과 : 그래서 예를 들어이 내 JSON 인 location_id의 출력으로 나오면 어떤 이유로

$.getJSON('data.json', function(data) { 
     var output = '<ul class="searchresults">'; 
     $.each(data, function(key, val) { 
      if (val.item.search(myExp) != -1) { 
       output += '<li>'; 
       output += '<p>' + "Category: " + val.category + '</p>'; 
       output += '<p>' + "Location: " + val.location_id + '</p>'; 
       output += '<p>' + "Business: " + val.business + '</p>'; 
       output += '</li>'; 
      } 
     }); 
     output += '</ul>'; 
     $('#update').html(output); 

나는 [개체, 개체] ... 수 누군가 내가 그 배열에있는 모든 것을 갖도록 도울까요?

감사합니다.

답변

2

JSON의 location_id 값은 "배열"이 아니며 "개체"입니다. Object의 기본 String 값은 [object Object]입니다.

output += '<p>' + "Location: " + 
    val.location_id.longitude + ', ' + 
    val.location_id.latitude + 
    '</p>'; 

을 또는 전체 location_id 개체를 타고 JSON을 캐릭터 라인 화 수 : 따라서, 당신은 전체 없습니다 객체의 다양한 조각으로 문자열을 CONCAT해야합니다. 그건 끔찍한 인간의 읽을 수 없을 것입니다 :

output += '<p>' + "Location: " + JSON.stringify(val.location_id) + '</p>'; 
관련 문제