kmathew
kmathew

Reputation: 9

How to parse JSON in PHP

I'm trying to parse JSON sent by POST to my webservice. I'm using Advanced Rest Client tool for Google Chrome to test restapi.

How can I read this request and response to it?

I'm sending key called "format" and "json" as value for this key. I'm adding JSON like

"{"id":"235325"}"

Part of my PHP API code:

if( strcasecmp($format,'json') == 0 )
{
   //how to read that id = 235325?
}

Upvotes: 0

Views: 103

Answers (2)

litelite
litelite

Reputation: 2851

Try the json_decode() function. It is the standard function to parse json in php.

Upvotes: 4

Vladimir Djukic
Vladimir Djukic

Reputation: 2072

If want to work with array:

$json = '{"id":"235325"}';

$array = json_decode($json, true);

foreach($array as $element){
if($element == 0){

}
}

With object:

$json = '{"id":"235325"}';

$object = json_decode($json);

if($object->id == 0){

}

Upvotes: 0

Related Questions