2014-03-26 2 views
0

일부 JSON 코드 분석 방법 :나는이 JSON 코드 샘플 분석하고자

<!DOCTYPE html><!--HTML5 doctype--> 
<html> 
<head> 
<title>Your New Application</title> 
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-  scale=1.0, minimum-scale=1.0, user-scalable=0" /> 
<style type="text/css"> 
    /* Prevent copy paste for all elements except text fields */ 
    * { -webkit-user-select:none; -webkit-tap-highlight-color:rgba(255, 255, 255, 0); } 
    input, textarea { -webkit-user-select:text; } 
    body { background-color:white; color:black } 
</style> 
<script src='intelxdk.js'></script> 
<script type="text/javascript"> 
    /* This code is used to run as soon as Intel activates */ 
    var onDeviceReady=function(){ 
    //hide splash screen 
    intel.xdk.device.hideSplashScreen(); 
    }; 
    document.addEventListener("intel.xdk.device.ready",onDeviceReady,false); 
</script> 
</head> 
<body> 
<a href="pays.json" target="_blank">Open JSON file</a><br /> 
<input type="button" value="Get and parse JSON" class="button" /> 
<br /> 
<span id="results"></span> 

<script src="libs/jquery-1.10.1.js"></script> 

<script> 

    //When DOM loaded we attach click event to button 
    $(document).ready(function() { 

     //after button is clicked we download the data 
     $('.button').click(function(){ 

      //start ajax request 
      $.ajax({ 
       url: "pays.json", 
       //force to handle it as text 
       dataType: "text", 
       success: function(data) { 

        //data downloaded so we call parseJSON function 
        //and pass downloaded data 
        var json = $.parseJSON(data); 
        //now json variable contains data in json format 
        //let's display a few items 
        $('#results').html('Plugin name: ' + json.licenses[0].id + '<br />Author: ' + json.licenses[0].value); 
       } 
      }); 
     }); 
    }); 
</script> 

</body> 
</html> 

PS : 나는 jQuery 플러그인을 사용하여 수행하는이 HTML 웹 페이지를

{ 
"licenses": [ 
    { 
    "id": "TN", 
    "value": "ar" 
    }, 
    {"id": "FR", "value": "fr"} , 
    {"id": "GB", "value": "en"} , 
    {"id": "US", "value": "en"} 
] 
} 

을 JSON 파일은 HTML 웹 페이지와 같은 폴더에 있습니다. 문제는 "JSON 가져 오기 및 파싱"버튼을 누르면 아무 것도 표시되지 않습니다.

+0

하여 Ajax 요청이 실행됩니다 있는지 확인하기 위해 불을 지르고 또는 유사한을 사용하여 시도하고 길을 따라 일종의 오류가있는 경우. – Cyclonecode

+0

'텍스트로 처리 할 수있는 힘 '. 왜? – dfsq

+0

JS 오류가 무엇입니까? 또한 코드의 많은 문제 : 1. jQuery와 전통적인 자바 스크립트를 혼합하여 사용합니다. 대신''을 사용하십시오; 3. CSS에서'*'를 사용하지 마십시오; 4. 작은 따옴표와 큰 따옴표 사용하기 – Raptor

답변

0

이 시도, 확실히 것이 도움이

$(document).ready(function() { 
    $('.button').click(function(){ 

     $.ajax({ 
      url: "pays.json", 
      dataType: "text", 
      success: function(data) { 
       $.each(data, function(idx, obj) { 
        alert(obj.id); 
       }); 
     }); 
    }); 
}); 
0

당신이 사용할 수있는 것 로그 문을 사용하여 어디에서 프로그램을 볼지 램이 도착하고 있으며, 여기에 해당 될 수있는 아약스 호출이 실패 할 경우 실행될 함수로 오류 함수를 제공 할 수도 있습니다. XDK 인텔의

$.ajax({ 
    url: "pays.json", 
    //force to handle it as text 
    dataType: "text", 
    error: function(x, status, msg) { console.log("Failed ajax request with status: " + status + " - " + msg); 
    success: function(data) { 

     console.log(data); 

     var json = $.parseJSON(data); 

     console.log(data); 

     $('#results').html('Plugin name: ' + json.licenses[0].id + '<br />Author: ' + json.licenses[0].value); 
    } 
}); 
+0

출력은 ** Uncaught SyntaxError : 예기치 않은 토큰 /index.html:1 ** – user1444393

+0

Google 크롬에서 테스트 한 결과 오류가 발생했습니다 : ** 리소스를로드하지 못했습니다 : Access-Control-Allow-Origin이 원본 Null을 허용하지 않음 file : ///home/myuser/Documents/pageweb/TestFailedPages/pays.json XMLHttpRequest가 file : ///home/myuser/Documents/pageweb/TestFailedPages/pays.json을로드 할 수 없습니다. Access-Control-Allow- 원산지 ** – user1444393

+0

또 다른 것은, 나는 새로운 인텔 XDK IDE와 함께 일하고 있으며, 나는 다음과 같은 tuto를 테스트했다. [lin k] http://runnable.com/UhY_jE3QH-IlAAAP/how-to-parse-a-json-file-using-jquery [link] ... 인텔 XDK 에뮬레이터로 테스트했는데, 작동하지만, 그렇지는 않습니다. Chrome :( – user1444393

0

내 솔루션 -> success:function(data, textStatus, request){ var d = JSON.stringify(data); alert('hello success : '+ d); var e = JSON.parse(d); alert('access_t:'+e.Access_token); }