从远程服务读取 json 数据并打印特定元素
Reading json data from remote service and printing specific elements
您好,我有这个 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
的值?
在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
如果你想用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"));
}
例如,您可以使用 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[
+ number + ]
,那么你可以像这样遍历它们:
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);
}
}
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;
}
您好,我有这个 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
的值?
在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
如果你想用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"));
}
例如,您可以使用 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[
+ number + ]
,那么你可以像这样遍历它们:
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);
}
}
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; }