2014-03-26 23 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); }