PHP parse data into an array - php

I am using a API to get some data from curl. Response i get is like:
{
"abc": 123,
"zxc": 122339900,
"cui": "usd",
"cumer": "wXl3tAPXCM",
"fee": 0,
"live": false,
"object": "test",
"paid": true,
"sss": {
"qwe": "4242",
"wer": "sss",
"mkm": "isa"
}
}
In which form it is ? how can i parse it to get same values in an array for the further process?
Any idea will be appreciated.

$new = json_decode($returnData);
This is a json, which can be converted into PHP array by the function json_decode

if the response is JSON you can use:
json_decode($response, true);
the second parameter forces the result to be an associative array.
php.net documentation

The response is in JSON
It's becoming the standard for transferring data from one machine to other because it is bandwidth friendly and language independent.
To translate the JSON into PHP array you can use PHP's native json_decode
$new = json_decode($returnData, TRUE);
Just be sure to give last param TRUE or you will get PHP Object instead of array.

This is a json string. You have to decode like below & you will get an array.
$your_array = json_decode($your_String);

The data is in 'JSON' format and you should have to use json_decode() function to decode the data..

This response is json. Use json_decode e.g.
$myarray = json_decode ($response, True);
Where the second argument True tells the function to produce an associative array.

Related

How to get label data from JSON from file contents

$json = file_get_contents('module=API&method=Referrers.getReferrerType&format=json&period=day&date=yesterday&disableLink=1&idSite=3');
$data = json_decode($json,true);
json data:
[{"label":"Direct Entry","nb_uniq_visitors":526,"nb_visits":593,"nb_actions":768,"nb_users":0,"max_actions":32,"sum_visit_length":83153,"bounce_count":513,"nb_visits_converted":0,"segment":"referrerType==direct"},{"label":"Search Engines","nb_uniq_visitors":230,"nb_visits":235,"nb_actions":631,"nb_users":0,"max_actions":71,"sum_visit_length":52233,"bounce_count":150,"nb_visits_converted":0,"segment":"referrerType==search","idsubdatatable":2},{"label":"Websites","nb_uniq_visitors":7,"nb_visits":7,"nb_actions":20,"nb_users":0,"max_actions":5,"sum_visit_length":835,"bounce_count":1,"nb_visits_converted":0,"segment":"referrerType==website","idsubdatatable":3}]
I'm trying to display each label for the referrer type.
Direct Entry
Search Engines
Websites
$data[0]->label; doesn't work
$data->label; doesn't work
$data->label[0]; doesn't work
You passed true to the second parameter to json_decode(). That makes it return an associative array. Thus, you can simply use the data like this :
$data[0]["label"]
You can try this, as you're converting the json data to associative array (nested), by passing true into the 2nd argument of json_decode method:
$json = "[{\"label\":\"Direct Entry\",\"nb_uniq_visitors\":526,\"nb_visits\":593,\"nb_actions\":768,\"nb_users\":0,\"max_actions\":32,\"sum_visit_length\":83153,\"bounce_count\":513,\"nb_visits_converted\":0,\"segment\":\"referrerType==direct\"},{\"label\":\"Search Engines\",\"nb_uniq_visitors\":230,\"nb_visits\":235,\"nb_actions\":631,\"nb_users\":0,\"max_actions\":71,\"sum_visit_length\":52233,\"bounce_count\":150,\"nb_visits_converted\":0,\"segment\":\"referrerType==search\",\"idsubdatatable\":2},{\"label\":\"Websites\",\"nb_uniq_visitors\":7,\"nb_visits\":7,\"nb_actions\":20,\"nb_users\":0,\"max_actions\":5,\"sum_visit_length\":835,\"bounce_count\":1,\"nb_visits_converted\":0,\"segment\":\"referrerType==website\",\"idsubdatatable\":3}]";
$data = json_decode($json, true);
print_f($data[0]['label']);
Hope this helps!

json_decode() php and how to reach variables in the array generated

I am decoding a json record in php, here is the structure :
{relations":[], "entity":
{"ide":1045, "status":"normal"}
}
Then my php code is :
$result = json_decode($json, true);
How to reach "ide" and "status" values from the array $result ?
Thanks a lot
Since you specified in json_decode that you want to have an associative array you can echo your data like this:
echo $result['entity']['ide'];
echo $result['entity']['status'];
With the second value set to true, your variables are in an array and so:
$result['entity']['ide'];
$result['entity']['status'];
If you miss out the true argument, your values would be in an object:
$result->entity->ide;
$result->entity->status;

How print json response in php

I have the json object from remote site. When I vardump the json response. The output looks like this..
object(GSResponse)#111 (7) {
["errorCode":"GSResponse":private]=>
int(0)
["errorMessage":"GSResponse":private]=>
NULL
["rawData":"GSResponse":private]=>
string(1808) "{
"UID": "*********",
}
]
}
How can I access the rawData parameter in the json response using php. Is there any function to convert it into php array.
I appreciate any help.
Edited - updated to include the comments
Answer
Lets say that $gsresponsevar is an object of type gsresponse, as defined below.
decode the json response-
$myjsonresponse= json_decode($gsresponsevar->getResponseText()) ;
Alternatively retrieve the var
echo $gsresponsevar->getString('uid');
Documentation
Extract from: http://developers.gigya.com/030_Server_SDKs/PHP/Reference/Class_GSResponse
string getString(string $key [, string $defaultValue])
this is the generic "frameworkless" native way
you can use JSON_decode to decode a JSON-string
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$dataObject = json_decode($json);
$dataArray = json_decode($json, true);
The second parameter defines whether you get an object (accessible via $dataObject->key) or an associative array (accessible via $dataArray['key']).
Be aware of the common mistakes mentioned in the API "Example #3 common mistakes using json_decode()"
This is the Gigya-API usage way
See the answer from Jason for more details for this
$responseObject->getString('key');
you can use json_decode($json_array);
for print your resulted array you can write var_dump(json_decode($json_array));
Thankx.

PHP JSON decode - stdClass

I was having a question about making a 2D JSON string
Now I would like to know why I can't access the following:
$json_str = '{"urls":["http://example.com/001.jpg","http://example.com/003.jpg","http://example.com/002.jpg"],"alts":["testing int chars àèéìòóù stop","second description",""],"favs":["true", "false", "false"]}';
$j_string_decoded = json_decode($json_str);
// echo print_r($j_string_decoded); // OK
// test get url from second item
echo j_string_decoded['urls'][1];
// Fatal error: Cannot use object of type stdClass as array
You are accessing it with array-like syntax:
echo j_string_decoded['urls'][1];
Whereas object is returned.
Convert it to array by specifying second argument to true:
$j_string_decoded = json_decode($json_str, true);
Making it:
$json_str = '{"urls":["http://site.com/001.jpg","http://site.com/003.jpg","http://site.com/002.jpg"],"alts":["testing int chars àèéìòóù stop","second description",""],"favs":["true", "false", "false"]}';
$j_string_decoded = json_decode($json_str, true);
echo j_string_decoded['urls'][1];
Or Try this:
$j_string_decoded->urls[1]
Notice the -> operator used for objects.
Quoting from Docs:
Returns the value encoded in json in
appropriate PHP type. Values true,
false and null (case-insensitive) are
returned as TRUE, FALSE and NULL
respectively. NULL is returned if the
json cannot be decoded or if the
encoded data is deeper than the
recursion limit.
http://php.net/manual/en/function.json-decode.php
json_decode by default turns JSON dictionaries into PHP objects, so you would access your value as $j_string_decoded->urls[1]
Or you could pass an additional argument as json_decode($json_str,true) to have it return associative arrays, which would then be compatible with $j_string_decoded['urls'][1]
Use:
json_decode($jsonstring, true);
to return an array.

Reading multiple json values with php

I am trying to read certain values from a json string in php, I am able to do a simple json string with only one value such as
$json = '{"value":"somevalue"}';
Using this:
<?php
$json = '{"value":"somevalue"}';
$obj = json_decode(json_encode($json));
print $obj->{'value'};
?>
But when i try an get a value from the following json string it throws an error...
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
I validated the json on JSONlint but not sure how to access the values within this with php.
Thanks
You can try this:
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
//since $json is a valid json format you needn't encode and decode it again
$obj = json_decode($json);
print_r($obj->filed);
print_r($obj->rule);
You can pass true as a second parameter to json_decode() to get the results as an array
$my_arr = json_decode($json, true);
var_dump($my_arr);
Should help you. You can then step through the array as you would normally.
use var_dump to print out the object with all it's members and hierarchy. you should then be able to find the value you are looking for

Categories