Created
September 20, 2014 11:39
-
-
Save fuzhengwei/bdfaaed18dd655c70909 to your computer and use it in GitHub Desktop.
json解析方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>json</title> | |
<script language="javascript" type="text/javascript"> | |
window.onload = function(){ | |
// 遍历数组对象 | |
$("blszdx").onclick = function(){ | |
$("tta").value = ""; | |
var ReturnInResult = | |
[ | |
{ | |
"PDA入库作业结果ID":"1001", | |
"入库单号":"1002", | |
"总件数":"1003", | |
"是否保税":"1004", | |
"入库类型":"1005", | |
"入库人":"1006", | |
"入库时间":"1007", | |
"备注":"1008", | |
"PCXX":[ | |
{ | |
"批次号":"2001", | |
"实入数量":"2002", | |
"重量":"2003", | |
"车牌号":"2004", | |
"集装箱号":"2005", | |
"WZXX":[ | |
{ | |
"仓库ID":"3001", | |
"垛位ID":"3002", | |
"码位ID":"3003", | |
"层级min":"3004", | |
"层级max":"3005" | |
} | |
] | |
} | |
] | |
}, | |
{ | |
"PDA入库作业结果ID":"1001", | |
"入库单号":"1002", | |
"总件数":"1003", | |
"是否保税":"1004", | |
"入库类型":"1005", | |
"入库人":"1006", | |
"入库时间":"1007", | |
"备注":"1008", | |
"PCXX":[ | |
{ | |
"批次号":"2001", | |
"实入数量":"2002", | |
"重量":"2003", | |
"车牌号":"2004", | |
"集装箱号":"2005", | |
"WZXX":[ | |
{ | |
"仓库ID":"3001", | |
"垛位ID":"3002", | |
"码位ID":"3003", | |
"层级min":"3004", | |
"层级max":"3005" | |
} | |
] | |
} | |
] | |
} | |
]; | |
/* 输出对象 | |
* json 为通用传输数据格式、解析方式统一 | |
* 下面为输出对象,如果能正常输出[Object Object]说明目前json正确 | |
*/ | |
alert("输出json对象;\r\n"+ReturnInResult); | |
/* | |
* 输出第一层第一行数据 | |
* 在js中,如果要输出json,那么需要使用eval函数,在C#中则使用C#方式 | |
*/ | |
alert("输出第一层第一行数据;\r\n"+eval('ReturnInResult')[0].PDA入库作业结果ID) | |
/* | |
* 输出第二层第一行数据 | |
*/ | |
alert("输出第二层第一行数据;\r\n"+eval('ReturnInResult')[0].PCXX[0].批次号); | |
/* | |
* 输出第三层第一行数据 | |
*/ | |
alert("输出第三层第一行数据;\r\n"+eval('ReturnInResult')[0].PCXX[0].WZXX[0].仓库ID); | |
/* | |
* 遍历整个json对象 | |
*/ | |
alert("遍历所有层级数据开始!"); | |
for(idOne in ReturnInResult){ | |
$("tta").value += "第"idOne+"组数据\r\n"; | |
$("tta").value += ReturnInResult[idOne].PDA入库作业结果ID+"\r\n"+ | |
ReturnInResult[idOne].入库单号+"\r\n"+ | |
ReturnInResult[idOne].总件数+"\r\n"+ | |
ReturnInResult[idOne].是否保税+"\r\n"+ | |
ReturnInResult[idOne].入库类型+"\r\n"+ | |
ReturnInResult[idOne].入库人+"\r\n"+ | |
ReturnInResult[idOne].入库时间+"\r\n"+ | |
ReturnInResult[idOne].备注 +"\r\n"; | |
for(idTwo in ReturnInResult[idOne].PCXX){ | |
$("tta").value += ReturnInResult[idOne].PCXX[idTwo].批次号+"\r\n"+ | |
ReturnInResult[idOne].PCXX[idTwo].实入数量+"\r\n"+ | |
ReturnInResult[idOne].PCXX[idTwo].重量+"\r\n"+ | |
ReturnInResult[idOne].PCXX[idTwo].车牌号+"\r\n"+ | |
ReturnInResult[idOne].PCXX[idTwo].集装箱号+"\r\n"; | |
for(idThree in ReturnInResult[idOne].PCXX[idTwo].WZXX){ | |
$("tta").value += ReturnInResult[idOne].PCXX[idTwo].WZXX[idThree].仓库ID+"\r\n"+ | |
ReturnInResult[idOne].PCXX[idTwo].WZXX[idThree].垛位ID+"\r\n"+ | |
ReturnInResult[idOne].PCXX[idTwo].WZXX[idThree].码位ID+"\r\n"+ | |
ReturnInResult[idOne].PCXX[idTwo].WZXX[idThree].层级min+"\r\n"+ | |
ReturnInResult[idOne].PCXX[idTwo].WZXX[idThree].层级max+"\r\n"; | |
} | |
} | |
} | |
} | |
} | |
function $(uid){ | |
return document.getElementById(uid); | |
} | |
</script> | |
</head> | |
<body> | |
<input type="button" value="遍历数组对象" id="blszdx"/> | |
<div id="divtext"></div> | |
<textarea id="tta" style="font-size=20px; width:500px; height=500px;"></textarea> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment