简体   繁体   中英

How to take particular value from json response in php

My Json String looks like this:

Now i wanted to take only value of ID as an Integer:

[{"Obj" :    
        { "ID":"11",
          "NAME":"XYZ",
          "GENDER":"M" 
        }
}]

How can i do this?

Try this,

<?php
    $json='[{"Obj" :    
            { "ID":"11",
              "NAME":"XYZ",
              "GENDER":"M" 
            }
    }]';
    $jsonArray=json_decode($json);
    echo $jsonArray[0]->Obj->ID;
?>

assuming that your json string is inside a post parameter:

$json_string = $_POST['json'];

using json_decode you can convert a json string to a php object:

$json = json_decode($json_string);

and then access to your ID:

$id = $json[0]->Obj->ID;

if you want to convert the object into associative array just do this:

$json = (array)$json;

and access to your id:

$id = $json[0]['Obj']['ID'];

Decode the json output using json_decode and put true as second parameter. It will give you array output.

$arr = json_decode($json,true);
echo $arr[0]['Obj']['ID'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM