Javascript SyntaxError的jQuery parseJSON帮助
我有如下JSON值:Javascript SyntaxError的jQuery parseJSON帮助,javascript,jquery,json,Javascript,Jquery,Json,我有如下JSON值: {"223":[{"virtuemart_state_id":"1","state_name":"Alabama"},{"virtuemart_state_id":"2","state_name":"Alaska"}]} <script type="text/javascript" src="jquery-1.11.1.min.js"></script> <script type="text/javascript"> $("do
{"223":[{"virtuemart_state_id":"1","state_name":"Alabama"},{"virtuemart_state_id":"2","state_name":"Alaska"}]}
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$("document").ready(function() {
var state,
url = 'http://localhost/jquery/test.json';
$.getJSON(url, function(data){
console.log(data);
$.each(data.223, function(i, rep){
state += "<option value = '" + rep.virtuemart_state_id + "'>" + rep.state_name + "</option>";
});
$("#state").html(state);
});
});
</script>
</head>
<div id="result">
<select id="state">
</select>
</div>
&我正在尝试解析如下数据:
{"223":[{"virtuemart_state_id":"1","state_name":"Alabama"},{"virtuemart_state_id":"2","state_name":"Alaska"}]}
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$("document").ready(function() {
var state,
url = 'http://localhost/jquery/test.json';
$.getJSON(url, function(data){
console.log(data);
$.each(data.223, function(i, rep){
state += "<option value = '" + rep.virtuemart_state_id + "'>" + rep.state_name + "</option>";
});
$("#state").html(state);
});
});
</script>
</head>
<div id="result">
<select id="state">
</select>
</div>
$(“文档”).ready(函数(){
var州,
url='1〕http://localhost/jquery/test.json';
$.getJSON(url、函数(数据){
控制台日志(数据);
$。每个(数据223,函数(i,代表){
州+=“”+rep.state_name+“”;
});
$(“#state”).html(state);
});
});
但它不适用于数字223&我得到的错误如下:SyntaxError:missing)在参数列表之后
知道我哪里出错了吗?谢谢您不能使用
运算符引用名称不是有效标识符的对象属性。相反,您应该这样做:
$.each(data[223], function(i, rep){
data.223
不是有效的Javascript。它必须是数据['223']
。
快捷方式符号很方便,但它无法处理您在JS中实际创建的所有可能的键名。尝试以下操作:$。每个(数据[223]
。您不能引用对象属性