0

Hi I have this json object data from service

{
    "Result": [
        {
            "Status": {
                "StatusCode": 200,
                "Text": "Successful"
            }
        },
        {
            "ListByPI": {
                "ORCA_ID": "25746",
                "ProtocolID": "20140476HU",
                "PIName": "DeFronzo"
            }
        },
        {
            "ListByPI": {
                "ORCA_ID": "21273",
                "ProtocolID": "20120202HU",
                "PIName": "DeFronzo"
            }
        }
    ]
}

How can I get the values of ORCA_ID, ProtocolID and PIName?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 2
    where you want to read means javascript, php or java – Manoj Sharma Jan 02 '15 at 10:21
  • 1
    That's a JSON object, you need to 'deserialize' it. What language are you using for your application? – Anto Jan 02 '15 at 10:23
  • Where you do want to parse this json?? In PHP or js or android? – Indra Kumar S Jan 02 '15 at 10:30
  • What do you want? You ask for a javascript solution and accept one with Java, then you ask more things in comments... You should read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – fedorqui Jan 02 '15 at 11:46

4 Answers4

0

In PHP you can use json_decode and extract like this

<?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

      ?>

And output is

In Result, first ORCA_ID is 25746
Indra Kumar S
  • 2,818
  • 2
  • 16
  • 27
0

If you want to parse it by using java, then you can use the following:

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"));
}
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
0

You can for example use a for and loop through your desired element, as seen in get keys of json-object in JavaScript:

for (k in s.Result[1].ListByPI) {
    document.write(k);   
}

See it in action:

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);   
}

Also in JSFiddle.


Update

If you don't want to specify Result[ + number + ], then you can loop through them like this:

for (i in s.Result) {
   document.write('we are in id);
   for (k in s.Result[i].ListByPI) {
       document.write(k);   
   }
}

See it in action:

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);   
   }
}
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
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;
            }