繁体   English   中英

从json文件中检索数据

[英]retrieve data from json file

我正在尝试从.json文件中检索数据,但使用jquery不会检索任何数据!

json文件数据:

[
{"optiontext" : "One", "optionvalue" : "One"},
{"optiontext" : "Two", "optionvalue" : "Two"},
{"optiontext" : "Three", "optionvalue" : "Three"}
]

我试图访问该数据的代码是:

<!doctype html>
<html>
    <head> 
      <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript"> 

    $(document).ready(function() {
            $('#submit').click(function () {
        $.ajax({

    //alert("I'm doing it now");
    //define the attributes for the connection
    type:"GET",
    url:"sample1.json",
    dataType:"json",
    //extrat the data

    success: function (data) {      
    var SampleFileMessage="<ul>";
    $.each(data, function(i,n){
    SampleFileMessage+="<li>"+n["optiontext"]+"</li>";
            });
    SampleFileMessage+="</ul>";
    $('#message').append(SampleFileMessage);
    document.writeln(SampleFileMessage);
}
});
return false;
});
});


    </script>
    </head>
    <body>
        <input type="button" onclick="getjson()" value="pressme"         style="align:center" />    
        <div id="messege"   > <input type="button" id="submit" value=" get the     data from json" ></div>
    </body>
    </html>

我该怎么处理这种json格式!

尝试这个:

$.each(data, function(i, n) {
    SampleFileMessage += "<li>" + n.optiontext + "</li>";
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM