我是JSON的新手,我使用json_encode创建一个看起来像这样的JSON对象
[{
"timestamp": "12\/16\/2013 0:00",
"curr_property": "7211",
"curr_property_cost": "123",
"day_property": "48",
"day_property_cost": "281",
"curr_solar_generating": "4958",
"curr_solar_export": "0",
"day_solar_generated": "33",
"day_solar_export": "0",
"curr_chan1": "1964",
"curr_chan2": "4958",
"curr_chan3": "289",
"day_chan1": "13",
"day_chan2": "33",
"day_chan3": "1"
}, {
"timestamp": "12\/16\/2013 0:00",
"curr_property": "7179",
"curr_property_cost": "123",
"day_property": "72",
"day_property_cost": "281",
"curr_solar_generating": "4926",
"curr_solar_export": "0",
"day_solar_generated": "49",
"day_solar_export": "0",
"curr_chan1": "1980",
"curr_chan2": "4926",
"curr_chan3": "273",
"day_chan1": "19",
"day_chan2": "49",
"day_chan3": "2"
}, {
"timestamp": "12\/16\/2013 0:00",
"curr_property": "9627",
"curr_property_cost": "165",
"day_property": "104",
"day_property_cost": "282",
"curr_solar_generating": "4749",
"curr_solar_export": "0",
"day_solar_generated": "65",
"day_solar_export": "0",
"curr_chan1": "1980",
"curr_chan2": "4749",
"curr_chan3": "2898",
"day_chan1": "26",
"day_chan2": "65",
"day_chan3": "12"
}, {
"timestamp": "12\/16\/2013 0:00",
"curr_property": "9610",
"curr_property_cost": "165",
"day_property": "136",
"day_property_cost": "282",
"curr_solar_generating": "4781",
"curr_solar_export": "0",
"day_solar_generated": "81",
"day_solar_export": "0",
"curr_chan1": "1980",
"curr_chan2": "4781",
"curr_chan3": "2849",
"day_chan1": "32",
"day_chan2": "81",
"day_chan3": "21"
}, {
"timestamp": "12\/16\/2013 0:01",
"curr_property": "9691",
"curr_property_cost": "166",
"day_property": "168",
"day_property_cost": "283",
"curr_solar_generating": "4797",
"curr_solar_export": "0",
"day_solar_generated": "97",
"day_solar_export": "0",
"curr_chan1": "1996",
"curr_chan2": "4797",
"curr_chan3": "2898",
"day_chan1": "39",
"day_chan2": "97",
"day_chan3": "31"
}, {
"timestamp": "12\/16\/2013 0:01",
"curr_property": "7034",
"curr_property_cost": "120",
"day_property": "191",
"day_property_cost": "283",
"curr_solar_generating": "4781",
"curr_solar_export": "0",
"day_solar_generated": "113",
"day_solar_export": "0",
"curr_chan1": "1980",
"curr_chan2": "4781",
"curr_chan3": "273",
"day_chan1": "46",
"day_chan2": "113",
"day_chan3": "32"
}]
Run Code Online (Sandbox Code Playgroud)
我试图使用下面的脚本解析数据
$(document).ready(
function() {
var jsonData = JSON.parse("<?php echo $jsondata; ?>");
console.log(jsonData.timestamp[0]);
});
Run Code Online (Sandbox Code Playgroud)
我不知道我在这里做错了什么.我知道javascript中默认长度为0,所以如何获取值?$ jsondata上的BTW var_dump给出了数据
您的JSON数据包含"字符.您正在尝试将其注入到分隔的JavaScript字符串文字中,"但您没有转义"数据中的字符.
您的JSON还包含新行,JavaScript字符串中不允许使用文字新行,因此您还需要使用转义序列(\n)替换它们.
也就是说,JSON是JavaScript文字语法的一个子集,因此您不需要将JSON文本转换为JavaScript字符串文字,然后解析它,您只需将JSON用作JavaScript:
var data = <?php echo $jsondata; ?>;
Run Code Online (Sandbox Code Playgroud)
你有另一个问题.您的JSON数据表示对象数组,而不是具有数组作为属性值的对象.您需要在属性名称之前访问该数组:data[0].timestamp.
| 归档时间: |
|
| 查看次数: |
60 次 |
| 最近记录: |