我正在尝试从.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格式!
答案 0 :(得分:0)
试试这个:
$.each(data, function(i, n) {
SampleFileMessage += "<li>" + n.optiontext + "</li>";
});