html 从JSON数组对象获取数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 从JSON数组对象获取数据相关的知识,希望对你有一定的参考价值。
<script>
/*
This is the example of how to get data from JSON Array Object
If you want to get earliest data, you need to know the data length first. Then use 'jsonobject.data.length-1' to get it.
Uncomment my example to see how it's work
DEMO: http://jsfiddle.net/bpphzwgx/
*/
var jsonobject = {
"meta":{
"author":"@h4ck4life",
"about":"Pos Laju Tracking API",
"email":"alifaziz@gmail.com",
"version":"0.0.4"
},
"data":[
{
"date":"03-Sep-2014",
"time":"14:52:58",
"process":"Delivered to ZULHILMI",
"office":"PPL KUANTAN"
},
{
"date":"03-Sep-2014",
"time":"08:01:28",
"process":"Delivery process",
"office":"PPL KUANTAN"
},
{
"date":"03-Sep-2014",
"time":"07:23:02",
"process":"Arrive at delivery office",
"office":"PPL KUANTAN"
},
{
"date":"02-Sep-2014",
"time":"22:06:51",
"process":"Consignment dispatch out from Transit Office",
"office":"PPL KUALA LUMPUR"
},
{
"date":"02-Sep-2014",
"time":"21:52:24",
"process":"Consignment arrived at Transit office",
"office":"PPL KUALA LUMPUR"
},
{
"date":"02-Sep-2014",
"time":"20:01:54",
"process":"Consignment dispatch out from Transit Office",
"office":"PPL KAJANG"
},
{
"date":"27-Aug-2014",
"time":"18:04:30",
"process":"Self collection at counter",
"office":"PPL KAJANG"
},
{
"date":"20-Aug-2014",
"time":"20:32:56",
"process":"Self collection at counter",
"office":"PPL KAJANG"
},
{
"date":"20-Aug-2014",
"time":"19:03:53",
"process":"Self collection at counter",
"office":"PPL KAJANG"
},
{
"date":"20-Aug-2014",
"time":"14:52:19",
"process":"Unsuccessful delivery to Addressee not available",
"office":"PPL KAJANG"
},
{
"date":"20-Aug-2014",
"time":"08:51:44",
"process":"Delivery process",
"office":"PPL KAJANG"
},
{
"date":"20-Aug-2014",
"time":"07:19:07",
"process":"Arrive at delivery office",
"office":"PPL KAJANG"
},
{
"date":"19-Aug-2014",
"time":"15:09:34",
"process":"Consignment received and processed from posting at Post Offices",
"office":"PPL KUANTAN"
}
]
};
// latest data
var latest_process = jsonobject.data[0].process;
var latest_date = jsonobject.data[0].date;
var latest_time = jsonobject.data[0].time;
var latest_office = jsonobject.data[0].office;
alert(latest_process);
alert(latest_date);
alert(latest_time);
alert(latest_office);
/* earliest data
var first_process = jsonobject.data[jsonobject.data.length-1].process;
var first_date = jsonobject.data[jsonobject.data.length-1].date;
var first_time = jsonobject.data[jsonobject.data.length-1].time;
var first_office = jsonobject.data[jsonobject.data.length-1].office;
alert(first_process);
alert(first_date);
alert(first_time);
alert(first_office);
*/
</script>
以上是关于html 从JSON数组对象获取数据的主要内容,如果未能解决你的问题,请参考以下文章
如何从 JSON 数组中的 JSON 对象中的单个值获取数据?