2014-03-26 25 views
0

我想解析此JSON代碼示例:如何分析一些JSON代碼

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

我有這樣的HTML網頁使用jQuery插件來做到這一點:

<!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:本JSON文件與HTML網頁位於相同的文件夾下。 問題是當我點擊「獲取並解析JSON」按鈕時,沒有任何顯示!

+0

嘗試使用螢火蟲或類似的,看看你的Ajax請求被執行,如果有某種沿途錯誤的。 – Cyclonecode

+0

'強制將其作爲文本處理。爲什麼? – dfsq

+0

什麼是JS錯誤?此外,代碼中還存在很多問題:1.混合使用jQuery和傳統JavaScript; 2.改用''; 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

您可以使用日誌語句來查看你的編程位置ram正在進入,並且還可能提供一個錯誤函數作爲一個函數,如果您的ajax調用失敗,將會執行這個函數,這可能是這裏的情況。

$.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

這裏是輸出**未捕獲的SyntaxError:意外的令牌/index.html:1 ** – user1444393

+0

我在Google Chrome上測試了它,我檢查了錯誤: **加載資源失敗:Access-Control-Allow-Origin不允許使用Origin null file: ///home/myuser/Documents/pageweb/TestFailedPages/pays.json XMLHttpRequest無法加載file:///home/myuser/Documents/pageweb/TestFailedPages/pays.json。Access-Control-Allow- Origin。** – user1444393

+0

另一件事,我正在使用新的intel XDK IDE,並且我測試了以下tuto:[lin k] http://runnable.com/UhY_jE3QH-IlAAAP/how-to-parse-a-json-file-using-jquery [鏈接] ...我測試了它與英特爾XDK模擬器,它的工作原理,但不是谷歌瀏覽器:( – user1444393

0

在XDK英特爾我的解決方案 - > 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); }