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?

4
  • 2
    where you want to read means javascript, php or java Commented Jan 2, 2015 at 10:21
  • 1
    That's a JSON object, you need to 'deserialize' it. What language are you using for your application? Commented Jan 2, 2015 at 10:23
  • Where you do want to parse this json?? In PHP or js or android? Commented Jan 2, 2015 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? Commented Jan 2, 2015 at 11:46

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

7 Comments

how to get all values of ORCA_ID,ProtocolID,PIName
what do you mean? Update your question indication what is exactly your desired output.
you code will display keys ORCA_ID,ProtocolID,PIName valuea but I want the values 25746,20140476HU,DeFronzo not the keys.
@RajaSekharReddyBolle then you can say: alert(k + " = " + s.Result[1].ListByPI[k]);. Check it and indicate if it works to you.
the above will work but everytime we have to pass Result[1 0r 2 or 3] values,how to get all data by passing dynamically values to Result{] array
|
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

1 Comment

I want to get in javascript
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"));
}

Comments

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

1 Comment

If the "ListByPI" repetition is not known then how we will get the values instead of passing 1 and 2 values to temp["Result"][1 or 2].ListByPI.ORCA_ID;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.