how to read below json data in php?
i have "$json = json_decode($data,true); and
i tryied "$json->{'screenShareCode'};" but is is giving me an error? :(
array(5) {
["screenShareCode"]=>
string(9) "887874819"
["appletHtml"]=>
string(668) ""
["presenterParams"]=>
string(396) "aUsEdyygd6Yi5SqaJss0="
["viewerUrl"]=>
string(65) "http://api.screenleap.com/v2/viewer/814222219?accountid=myid"
["origin"]=>
string(3) "API"
}
The output you are showing is not json. It seems to be a print_r'ed array.
See http://json.org/example
Your output is regular array not JSON, so you access it as regular PHP array:
$x = $array['screenShareCode']
You are looking for json_encode (http://de2.php.net/json_encode)
What you posted is an array, not an object. Because you passed json_decode a second parameter of true, it responded with an associative array instead of an object.
To access a property of an associative array, you can do something like $json['screenShareCode'].
Related
i have a json file, that has to be parsed in a loop.
i cant seem to succeed
JSON:
{"IMD":{"url":"http:\/\/www.google.com","timeOut":1515155361},"cvH":{"url":"http:\/\/www.google.com","timeOut":1515155364}}
PHP:
<?php
$linkyValues="./linky.json";
if (file_exists($linkyValues)) {
$fileStream = fopen($linkyValues, 'r');
$fileValue=json_decode(fread($fileStream, filesize($linkyValues)));
fclose($fileStream);
echo count($fileValue);//Always 1!
for($i=0;$i<count($fileValue);$i++){
$timeout=$fileValue->item($i)->timeOut;
if(time()>=$timeout){
unset($fileValue[$i]);
}
}
$fileStream = fopen($linkyValues, 'w');
fwrite($fileStream, json_encode($fileValue));
fclose($fileStream);
}
?>
my problem is that count($fileValue) is always 1.
Output of var_dump($fileValue):
object(stdClass)#2 (2) {
["IMD"]=>
object(stdClass)#1 (2) {
["url"]=>
string(21) "http://www.google.com"
["timeOut"]=>
int(1515155361)
}
["cvH"]=>
object(stdClass)#3 (2) {
["url"]=>
string(21) "http://www.google.com"
["timeOut"]=>
int(1515155364)
}
}
it looks like an array to me...
JSON does not support the concept of an associative array, but stores such maps as objects instead.
Your JSON file contains such an object. For PHP this means, that it can either import it as an stdClass ( object ) or as an associatiave array.
This is decided by the json_decode's second parameter, that is either TRUE to read the object as an associative array, or FALSE to read it as an object.
Therefore this will fix your problem:
$fileValue = json_decode(fread($fileStream, filesize($linkyValues)), TRUE);
json_decode documentation
In addition to that, your code has problems with iterating the array. You use $fileValue->item($i) as well as $fileValue[$i], while you have an associative array.
You work with it as if it was an indexed array, while it is an associative array, which means it has keys instead of indices, that identify the values in your array.
The propper way to iterate an associative array is with foreach, like deomstrated belo:
foreach($fileValue as $key => $value) {
if (time() >= $value['timeOut']) {
unset($fileValue[$key]);
}
}
Yet, since you only want to remove specific values, you can use array_filter as well:
$fileValue = array_filter($fileValue, function($value){
return time() < $value['timeOut'];
});
array_filter will then take care of removing the specified fields from your array, so you do not have to unset them manually.
You can use :
count((array)$fileValue);
You're mixing up arrays and objects here.
count() can only be used on arrays, however you can cast an object to array to achieve the same thing.
$fileValue[$i] is a method to access an array, which won't work with your json object.
I see a solution already is to just change your object to an array so I'd like to offer the solution if you wanted to stick with objects.
$linkyValues="./linky.json";
if (file_exists($linkyValues)) {
$fileStream = fopen($linkyValues, 'r');
$fileValue=json_decode($jsonString);
fclose($fileStream);
//Cast the object to an array to get the count, but count isn't really requierd
echo count((array)$fileValue);
//loop through the object
foreach($fileValue as $key=>$fv){
//pull the timeout
$timeout=$fv->timeOut;
//do the check
if(time()>=$timeout){
//remove the timeout from the object
unset($fileValue->$key);
}
}
$fileStream = fopen($linkyValues, 'w');
fwrite($fileStream, json_encode($fileValue));
fclose($fileStream);
}
?>
I'm working on an AJAX CRUD and I cannot get the form values in the Assoc. array to save individually as object attributes for the MySQL query.
I am following enter link description here but instead of the mysqli I'm using PDO.
Not much of a php person and this is my first OOP use of PDO and JSON.
The vardump() shows the input text is there...
// get posted data
$data = json_decode(file_get_contents("php://input"), true);
// set event property values
$event=>mainTitle = $data->main-title;
$event->subTitle = $data->sub-title;
$event->eventUrl = $data->event-url;
And the dumps:
array(9) {
["main-title"]=>
string(15) "Test Main Title"
["sub-title"]=>
string(14) "Test Sub title"
["event-url"]=>
string(9) "Test URTL"
...
object(Event)#3 (11) {
["conn":"Event":private]=>
object(PDO)#2 (0) {
}
["table_name":"Event":private]=>
string(8) "tblEvent"
["mainTitle"]=>
int(0)
["subTitle"]=>
int(0)
["eventUrl"]=>
int(0)
...
try changing $event=>mainTitle to $event->mainTitle
You have passed a second argument to json_decode() what means you'd like to get an array instead of the object. So just work like with the array. Replace to $event->mainTitle = $data['main-title'];
I found the answer for part of my problem here: Handling-JSON-like-a-boss-in-PHP
json_decode() is able to return both an object or associative array.
//For an object:
$result = json_decode($string);
//For an Assoc. Array:
$result = json_decode($string, true);
What I struggled with is that the var_dump() returns almost exact result for the two. They indeed have to be the same type.
The second part of my problem that was more subvert was having hyphens in the object attribute names. I didn't find a reason why but for sake of clarity in my code I just removed them.
i have an array after convert it from xml data and i have var_dump the data in array like :
object(SimpleXMLElement)#651 (1) {
["authenticationSuccess"]=>
object(SimpleXMLElement)#652 (1) {
["user"]=>
string(9) "yassine"
}
}
i want to get the value the attribut user that equal "yassine" in this cas .
i trying
$xml["authenticationSuccess"]["user"]
but not working , it return null value , is there any solution to get this data from the array .
some help please
It seems your variable is not array but object, so you need to use $xml->authenticationSuccess->user;
As the var_dump says, you have an object instead of an associative array. You can access object fields like this:
$xml->authenticationSuccess->user;
or this:
$xml->{"authenticationSuccess"}->{"user"};
I'm not able to get the value of the image attribute in this (apparently valid) JSON object:
echo var_dump($result);
array(1) {
["images"]=>
array(1) {
[0]=>
array(1) {
["src"]=>
string(112) "http://staticf5a.diaadia.info/sites/default/files/styles/landscape_310_155/public/nota_periodistica/taxis_13.jpg"
}
}
}
$jsonResult = json_encode($result); //result is an array of arrays
echo $jsonResult;
{"images":[{"src":"http:\/\/staticf5a.diaadia.info\/sites\/default\/files\/styles\/landscape_310_155\/public\/nota_periodistica\/taxis_13.jpg"}]}
echo $jsonResult->images; //show nothing
This snippet was working few days ago, and logs (ini_set('display_errors', '0');) don't show anything related.
After encoding, $jsonResult is just a string and you won't be able to access any elements of encoded JSON without decoding it first.
Have a look at PHP's ``json_decode'' function: http://php.net/manual/pl/function.json-decode.php
It will convert the JSON back to associative array or object.
Anyway, I have no idea why you encode the associative array as JSON and try to access images there instead of just taking it from the array itself.
You are trying to get property from a string here. json_encode() returns the json representation of an object as a string. That string you can turn into an actuall object with json_decode()
So, i'm trying to access some input fields in a form which I get with
$data = json_encode($app->request->getBody());
It returns something like this after an echo $data:
"groupname=group13&description=description"
But I just can't figure out how to access those parameters, I've tried with
$data[0]
$data['groupname']
$data->groupname
How can I access to this array elements?
[UPDATE]
Tried all of your solutions, none of them worked for me :(
I've finally solved it using this since I only have 2 fields
$groupname = $app->request->post('groupname');
$description = $app->request->post('description');
That is not JSON, it's URL encoded text... Use parse_str() instead.
Friend, in this case if you really need to manage this final query string you can do:
$my_query = "groupname=group13&description=description";
explode elements into a assoc array using parse_str:
parse_str($my_query, $output);
var_dump of $output :
array(2) {
'groupname' =>
string(7) "group13"
'description' =>
string(11) "description"
}
you can remount your query string too using http_build_query:
var_dump(http_build_query($output));
string(41) "groupname=group13&description=description"
I hope this can help you.
Seems like you executed the serialize() method in jQuery
When you do a json_encode(), the output is like {"a":1,"b":2,"c":3,"d":4,"e":5}