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);
Related
i have searched and searched but i don't know where i'm wrong getting a value from JSON encode, can you help me? Please don't kill me, i'm a newbie :)
My php:
<?php
$data = json_decode("document.json", true);
$getit = $data["likes"];
My JSON:
[{
"title" : "MYTITLE",
"image" : "MYIMAGE",
"likes" : 0
}]
EDIT
Thanks for the help this is now working!
json_decode expects an string, not an filename - so you have first get the contents of the given file. This could be easily achieved with file_get_contents.
Your current json structure contains an array(with currently only one element), which contains an object. So if you want the likes, you have to read the first element of the result array and of that(an associative array), the likes.
$data = json_decode(file_get_contents($filename), true);
$likes = $data[0]['likes'];
If you have more than one object in the given json file, you could loop over the data with foreach
foreach ($data as $element) {
echo "likes of {$element['title']}: {$element['likes']}\n";
}
For json_decode you have to pass JSON string, not a file name. Use file_get_contents to get JSON content and then decode it.
$data = json_decode(file_get_contents('document.json'), true);
$getit = $data[0]['likes'];
Your JSON is an array of objects, you first need to access the first element of your array :
$data[0]
Then you can access the key you want :
$getit = $data[0]['likes'];
Plus, as stated in a comment above, you need to pass a string to json encode, here is an code sample that should suit your needs :
<?php
$data = json_decode(file_get_contents('document.json'), true);
$getit = $data[0]["likes"];
Hope this helps.
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 ....
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 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.
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