嗨,我有来自服务的这个json对象数据
{
"Result": [
{
"Status": {
"StatusCode": 200,
"Text": "Successful"
}
},
{
"ListByPI": {
"ORCA_ID": "25746",
"ProtocolID": "20140476HU",
"PIName": "DeFronzo"
}
},
{
"ListByPI": {
"ORCA_ID": "21273",
"ProtocolID": "20120202HU",
"PIName": "DeFronzo"
}
}
]
}
如何获取ORCA_ID
,ProtocolID
和PIName
的值?
答案 0 :(得分:0)
在PHP中,您可以使用json_decode并像这样提取
<?php
$contents = '{
"Result":[
{
"Status":{
"StatusCode":200,
"Text":"Successful"
}
},
{
"ListByPI":{
"ORCA_ID":"25746",
"ProtocolID":"20140476HU",
"PIName":"DeFronzo"
}
},
{
"ListByPI":{
"ORCA_ID":"21273",
"ProtocolID":"20120202HU",
"PIName":"DeFronzo"
}
}
]
}';
$json = json_decode($contents, true);
echo "In Result first ORCA_ID is ".$json['Result'][1]['ListByPI']['ORCA_ID']."<br>"; //Here result is an array...So Indexs should be mentioned... then string name
?>
输出
In Result, first ORCA_ID is 25746
答案 1 :(得分:0)
如果要使用java解析它,则可以使用以下命令:
String json = "{\"Result\":[{\"Status\":{\"StatusCode\":200,\"Text\":\"Successful\"}},{\"ListByPI\":{\"ORCA_ID\":\"25746\",\"ProtocolID\":\"20140476HU\",\"PIName\":\"DeFronzo\"}},{\"ListByPI\":{\"ORCA_ID\":\"21273\",\"ProtocolID\":\"20120202HU\",\"PIName\":\"DeFronzo\"}}]}";
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("Result");
for (int i = 1; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i).getJSONObject("ListByPI");
System.out.println("ORCA_ID = " + object.getString("ORCA_ID") + " --- ProtocolID = " + object.getString("ProtocolID"));
}
答案 2 :(得分:0)
您可以使用for
并循环浏览所需的元素,如get keys of json-object in JavaScript中所示:
for (k in s.Result[1].ListByPI) {
document.write(k);
}
看到它的实际效果:
var s = {
"Result": [
{
"Status": {
"StatusCode": 200,
"Text": "Successful"
}
},
{
"ListByPI": {
"ORCA_ID": "25746",
"ProtocolID": "20140476HU",
"PIName": "DeFronzo"
}
},
{
"ListByPI": {
"ORCA_ID": "21273",
"ProtocolID": "20120202HU",
"PIName": "DeFronzo"
}
}
]
};
for (k in s.Result[1].ListByPI) {
document.write(k);
}
同样在JSFiddle。
如果您不想指定Result[
+数字+ ]
,那么您可以像这样循环播放它们:
for (i in s.Result) {
document.write('we are in id);
for (k in s.Result[i].ListByPI) {
document.write(k);
}
}
看到它的实际效果:
var s = {
"Result": [
{
"Status": {
"StatusCode": 200,
"Text": "Successful"
}
},
{
"ListByPI": {
"ORCA_ID": "25746",
"ProtocolID": "20140476HU",
"PIName": "DeFronzo"
}
},
{
"ListByPI": {
"ORCA_ID": "21273",
"ProtocolID": "20120202HU",
"PIName": "DeFronzo"
}
}
]
};
for (i in s.Result) {
document.write('we are in id);
for (k in s.Result[i].ListByPI) {
document.write(k);
}
}
答案 3 :(得分:0)
You can write this: var temp = {"Result": [ { "Status": { "StatusCode": 200, "Text": "Successful" } }, { "ListByPI": { "ORCA_ID": "25746", "ProtocolID": "20140476HU", "PIName": "DeFronzo" } }, { "ListByPI": { "ORCA_ID": "21273", "ProtocolID": "20120202HU", "PIName": "DeFronzo" } }] }; temp["Result"][0].Status.StatusCode; temp["Result"][0].Status.Text; temp["Result"][1].ListByPI.ORCA_ID; temp["Result"][1].ListByPI.ProtocolID; temp["Result"][1].ListByPI.PIName; temp["Result"][2].ListByPI.ORCA_ID; temp["Result"][2].ListByPI.ProtocolID; temp["Result"][2].ListByPI.PIName; }