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.
Related
I have a JSON that's strangely formatted ...but it's the way I receive it. Because the arrays inside are huge, simply copying and pasting it takes a long time, so I'm wondering if there's a PHP way to do it.
The way I get it is like this:
{"count":459,"results":[{"title":"Something ....}],"params":{"limit..},"type":"Listing","pagination":{"..":5}}
But I want to get only the "results" array, basically the part of [{"title":"Something ....}]. How would I do that?
Do
$arr = json_decode(your_json, true);
If you ned it as JSON again, do
json_encode($arr['results']);
You can get to that part as follows:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit":".."},"type":"Listing","pagination":{"..":5}}';
$obj = json_decode($json);
echo $obj->results[0]->title;
Outputs:
Something ....
You have to decode your json. Be sure that the json is valid!
Your decoded json returns an object of type stdClass instead of an array!
See below the tested code:
$json = '{"count":459,"results":[{"title":"Something ...."}],"params":{"limit": "foo"},"type":"Listing","pagination":{"foo":5}}';
$decoded = json_decode($json);
So you can access array results from the object $decoded:
print_r($decoded->results);
But, in the array, there are objects, thus:
echo $decoded->results[0]->title; // Something ....
Im trying to read a value of an ajax send jsonarray to base my filename upon but i cant seem to figure out how to read the value.
my php;
$postdata = $_POST['data'];
$jsondata = json_decode($postdata);
$myname = $jsondata->name;
$dir = 'users/'.$myname.'/desktop-'.$myname.'.json';
json array looks like this;
[{"name":"mmichel"},
{"myicons":
[{"icon":
[{"name":"homepagelink","rel":"http://test.tocadovision.nl","id":"icon1","class":"icon bookmark"}]
},
{"icon":
[{"name":"aboutpagelink","rel":"http://test.tocadovision.nl","id":"icon2","class":"icon bookmark"}]
}]
}]
hope someone can tell me what im doing wrong.. must be rly easy i guess
Since $jsondata contains an array of one object, you need to access the first element of the array in your assignment:
$myname = $jsondata[0]->name;
Ensure you have json enabled in your php config.
You can do this by doing
<?php
phpinfo();
This should output something like this which indicates json module is enabled. If you don't have this enabled, enable it.
Make sure you have the correct valid json string in $json variable and use one of the following methods.
$arr = json_decode($json, true);
print_r($arr[0]['name']);
$arr = json_decode($json);
print_r($arr[0]->name);
I'm writing a simple PHP script that makes an API call that returns JSON. However, I'm having trouble figuring out how to take that JSON string, convert it into a dictionary, and access nested dictionaries/data within it.
Here's what I have so far:
<?php
$id = $_REQUEST['id'];
$url = http://exampleURLThatReturnsJSONString.com
$rawData = file_get_contents($url);
I've read that you should use something like $decodedData = json_decode($rawData)
, but I'm not sure what to do next, especially if I want to access nested dictionaries with a key like Schedule.
Any help would be greatly appreciated, thanks!
json_decode($json, $assoc = false) converts the json string into an object by default, or into an array if you specify $assoc = true
If you have $assoc = false then you must access the values by $decoded_data->key. Whereas if you have $assoc = true then you can do $decoded_data['key']
You can just access the decoded data like this:
echo $decodedData['key'];
This wil echo the value of the item in the dictionary with key 'key'. Nested values can be accessed like this:
echo $decodedData['key1']['key2']['...'];
You can always use var_dump to show what's inside the result. Also, read the documentation on json_decode for more information.
json_decode return array or object to which access is like to array.
http://php.net/manual/en/function.json-decode.php
I'm getting below JSON response:
[{"startDate":"2012-07-12 11:21:38 +0530","totalTime":0},{"startDate":"2012-07-11 11:27:33 +0530","totalTime":0},{"startDate":"2012-07-16 18:38:37 +0530","totalTime":0},{"startDate":"2012-07-17 14:18:32 +0530","totalTime":0}]
i want make array of start date and totalTime, i have used these two lines but it wont work $obj, please suggest..
$obj = json_decode($dateTimeArr);
$dateAr = $obj->{'startDate'};
As everyone said, and you did - use json_decode.
$dateArrays = json_decode($dateTimeArr, true); // decode JSON to associative array
foreach($dateArrays as $dateArr){
echo $dateArr['startDate'];
echo $dateArr['totalTime'];
}
In future, if you are unsure what type or structure of data is in the variable, do var_dump($var) and it will print type of variable and its content.
It is very easy:
$Arr = json_decode($JSON, true);
json_decode() will give you nested PHP types you can then descend to retrieve your data.
use json_decode($json_response,true) to convert json to Array
Guess what you are looking for is json_decode()
Check out http://php.net/manual/en/function.json-decode.php for the inner workings
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