Json, PHP, and sessions - php

I would like to print a value from the following JSON in PHP. How would I access a specific value? Also this array is stored in a session call forminfo. So I need to access the session then the specific value of FirstName for example.
Array (
[forminfo] => stdClass Object (
[GetProspectAsJSONResult] => [
{
"ProspectId": xxxxx,
"Keycode": "Test123",
"FirstName": "Test",
"LastName": "Test",
"Company": "Test",
"Email": null
}
]
)
)
Thanks!

you should use json_decode on object which decodes it and returns an array. then you could for example access Firstname with decodedArray['FirstName']

Related

I'm trying to decode json array to php but it's returning a blank page

I'm trying to decode json array to php array using json_decode, but it's displaying a blank page
Here's the json array
[["id":"2","name":"Sam Nju","photo":"1510074080885.jpg","qty":"10","price":"10000.00"],["id":"3","name":"Daniel","photo":"1510074047056.jpg","qty":"0","price":"40000.00"]]
Here's the code to decode json
$json = file_get_contents('http://localhost/example/index.php/destinations/json');
$data = json_decode($json,true);
$names = $data['result'];
echo "<pre>";
echo $names;
print_r($names);
Thanks
For obtaining proper JSON using json_decode() and json_encode() follow these guidelines:
json_decode() :
This function only works with UTF-8 encoded strings.
Returns the value encoded in json in appropriate PHP type. Values true, false and null 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.
The key and value must be enclosed in double quotes single quotes are not valid.
Your JSON:
[["id":"2","name":"Sam Nju","photo":"1510074080885.jpg","qty":"10","price":"10000.00"],["id":"3","name":"Daniel","photo":"1510074047056.jpg","qty":"0","price":"40000.00"]]
apears to be invalid. Arrays use [] while objects use {}.
This is an example of how a proper PHP array structure would look like prior to doing json_encode() (before sending):
// array structure in PHP to get proper JSON
Array ( [0] => Array ( [id] => 2 [name] => Sam Nju [photo] => 1510074080885.jpg [qty] => 10 [price] => 10000.00 ) [1] => Array ( [id] => 3 [name] => Daniel [photo] => 1510074047056.jpg [qty] => 0 [price] => 40000.00 ) )
which was obtained using the following:
json_decode('[{"id":"2","name":"Sam Nju","photo":"1510074080885.jpg","qty":"10","price":"10000.00"},{"id":"3","name":"Daniel","photo":"1510074047056.jpg","qty":"0","price":"40000.00"}]', true)
which would mean doing this:
$myArray = array();
$firstPerson = array();
$secondPerson = array();
$firstPerson['id'] = 2;
$firstPerson['name'] = "Sam Nju";
// ...
$secondPerson['id'] = 3;
$firstPerson['name'] = "Daniel";
// ...
array_push($myArray, $firstPerson);
array_push($myArray, $secondPerson);
// or $myArray[0] = $firstPerson; and $myArray[1] = $secondPerson;
While valid JSON would look like this:
{{"id":"2","name":"Sam Nju","photo":"1510074080885.jpg","qty":"10","price":"10000.00"},{"id":"3","name":"Daniel","photo":"1510074047056.jpg","qty":"0","price":"40000.00"}}
If you are getting the data from a database you might want to use something like this:
$result = mysqli_query($con, "SELECT .... // database query, $con is connection variable
$myArray = array();
while($row = mysqli_fetch_array($result))
{
$tempArray = array();
$tempArray['id'] = $row[0];
$tempArray['name'] = $row[1];
$tempArray['photo'] = $row[2];
// ...
array_push($myArray, $tempArray);
}
// use print_r($myArray); to test it out.
Although your code looks correct, your JSON data is invalid. Objects are enclosed by {}, not []. Replace the JSON with this, and it should work.
[
{
"id": "2",
"name": "Sam Nju",
"photo": "1510074080885.jpg",
"qty": "10",
"price": "10000.00"
},
{
"id": "3",
"name": "Daniel",
"photo": "1510074047056.jpg",
"qty": "0",
"price": "40000.00"
}
]
Also above answer already mentioned it:
Your JSON is invalid. You can check this e.g. with an JSON linter like https://jsonlint.com/
Plus, you're referencing names with $names = $data['result'];. However, in your provided JSON there is no array (or better object), with key "result".
You may look up your PHP's error log file to understand where the problem lies.

Defining multidimensional array while storing form data in JSON format in php

I have to convert form data to JSON format. I am trying to achieve this:-
{"appConfiguration" : {
"configuration_name" = "AS400 Configuration",
"configuration_version" = "1.001",
"connection" : [ {
"ip_address" : [ “10.10.10.01”,
“10.10.10.02”,
“10.10.10.03”
// all saved IP Address.
]
"port" : "23"
"ssl" : "NO",
"device_name" : "Agicent Device",
"name" : "Puga",
"user" : "smart gladiator",
"password" : "sgl2013",
"barcode_enter" : "NO",]}}
This is what my JSON should look like. I am able to store data in single-dimension array; how do I create a structure like this?
"connection":["ohiuh","ghu","ip_address":["something","something","something"]]
Try with this
$arr = array('connection'=>array("ohiuh","ghu" , json_encode(array("ip_address"=>array("something","something","something")))));
echo json_encode($arr);
To get for example ip_address you can do this:
$array = json_decode($jsonstring);
echo $array['connection']['ip_address']['something'];
This will decode your json string into an multidimensional array and than you can simply echo it.
To encode it:
$test = array("appConfiguration" => array("configuration_name"=> "AS400 Configuration", "configuration_version" => "1.001", "connection"=> array("ip_address" => array('10.10.10.01', '10.10.10.02', '10.10.10.03'), "port" => "23",
"ssl" => "NO",
"device_name" => "Agicent Device",
"name" => "Puga",
"user" => "smart gladiator",
"password" => "sgl2013",
"barcode_enter" => "NO")));
echo(json_encode($test));
To use data you get from a form you can do this:
$array = array('connection'=>array($_POST["ohiuh"],$_POST["ghu"] , array("ip_address"=>array($_POST["ip_adress1"],$_POST["ip_adress2"],$_POST["ip_adress3"])))));
echo json_encode($array);
Write a form with the value you need and than post them. Than create the array with the $_POST["something"] values and encode this array to json with json_encode();
Hope this is now the answer to your question.

extract specific data from json array using php curl [duplicate]

this is what i get as a string from a feed finder url (JSON Encoded):
{
"updated": 1265787927,
"id": "http://www.google.com/reader/api/0/feed-finder?q\u003dhttp://itcapsule.blogspot.com/\u0026output\u003djson",
"title": "Feed results for \"http://itcapsule.blogspot.com/\"",
"self": [{
"href": "http://www.google.com/reader/api/0/feed-finder?q\u003dhttp://itcapsule.blogspot.com/\u0026output\u003djson"
}],
"feed": [{
"href": "http://itcapsule.blogspot.com/feeds/posts/default"
}]
}
How can i decode it using json_decode() function in php and get the last array element ("feed") ? i tried it with the following code but no luck
$json = file_get_contents("http://www.google.com/reader/api/0/feed-finder?q=http://itcapsule.blogspot.com/&output=json");
$ar = (array)(json_decode($json,true));
print_r $ar;
Please help ..
$array = json_decode($json, true);
$feed = $array['feed'];
Note that json_decode() already returns an array when you call it with true as second parameter.
Update:
As the value of feed in JSON
"feed":[{"href":"http://itcapsule.blogspot.com/feeds/posts/default"}]
is an array of objects, the content of $array['feed'] is:
Array
(
[0] => Array
(
[href] => http://itcapsule.blogspot.com/feeds/posts/default
)
)
To get the URL you have to access the array with $array['feed'][0]['href'] or $feed[0]['href'].
But this is basic handling of arrays. Maybe the Arrays documentation helps you.

how to decode this JSON string?

this is what i get as a string from a feed finder url (JSON Encoded):
{
"updated": 1265787927,
"id": "http://www.google.com/reader/api/0/feed-finder?q\u003dhttp://itcapsule.blogspot.com/\u0026output\u003djson",
"title": "Feed results for \"http://itcapsule.blogspot.com/\"",
"self": [{
"href": "http://www.google.com/reader/api/0/feed-finder?q\u003dhttp://itcapsule.blogspot.com/\u0026output\u003djson"
}],
"feed": [{
"href": "http://itcapsule.blogspot.com/feeds/posts/default"
}]
}
How can i decode it using json_decode() function in php and get the last array element ("feed") ? i tried it with the following code but no luck
$json = file_get_contents("http://www.google.com/reader/api/0/feed-finder?q=http://itcapsule.blogspot.com/&output=json");
$ar = (array)(json_decode($json,true));
print_r $ar;
Please help ..
$array = json_decode($json, true);
$feed = $array['feed'];
Note that json_decode() already returns an array when you call it with true as second parameter.
Update:
As the value of feed in JSON
"feed":[{"href":"http://itcapsule.blogspot.com/feeds/posts/default"}]
is an array of objects, the content of $array['feed'] is:
Array
(
[0] => Array
(
[href] => http://itcapsule.blogspot.com/feeds/posts/default
)
)
To get the URL you have to access the array with $array['feed'][0]['href'] or $feed[0]['href'].
But this is basic handling of arrays. Maybe the Arrays documentation helps you.

Issue with JSON and jQuery Iteration

I'm pretty sure it's an obvious error somewhere on this - but let me explain what I'm donig :
I'm calling a PHP file via jQuery as follows :
$.getJSON("/phpincs/getbucket.php?awskey="+awskey+"&awssecret="+awssecret+"&bucket="+bucket,
function(json){
$.each(json,function(i,item){
$(new Option(item.name,item.name)).appendTo('#bucket_contents_0');
});
}
and the JSON file it returns is as follows :
Array
(
[bt_shop.png] => Array
(
[name] => bt_shop.png
[time] => 1260393948
[size] => 156985
[hash] => 8a4eba621d5e08b84c962a0ad21ec2ae
)
[me_profile.jpg] => Array
(
[name] => me_profile.jpg
[time] => 1260393952
[size] => 2714
[hash] => 4f5d185b0c671e6165902762681f098b
)
[thumbnail.aspx.jpeg] => Array
(
[name] => thumbnail.aspx.jpeg
[time] => 1260393951
[size] => 5268
[hash] => 6939efa58ff7ffcac17218ffdd0d5d8c
)
)
true
for some reason it doesn't seem to fire the function(json){} - I've stuck an alert(''); in and it doesn't do anything.
Can someone quickly explain to me what seems to be going wrong there?
Cheers,
Carl
It is more than likely not calling the callback function because it doesn't look like what you are returning is json. If you have an $variable defined that contains your array...call
echo json_encode($jsondata); exit;
At the end of your script.
I've changed the names of the inner arrays as your previous labels are going to cause problems with the dot. You will get an error like:
myArray.bt_shop is undefined
when you try to call
alert(myArray.bt_shop.png.name);
the only way it could be called is with
alert(myArray["bt_shop.png"].name);
So having changed your code a bit, this is the JSON version of your arrays...
{
"one":
{
"name": "bt_shop.png",
"time": "1260393948",
"size": "156985",
"hash": "8a4eba621d5e08b84c962a0ad21ec2ae"
},
"two":
{
"name": "me_profile.jpg",
"time": "1260393952",
"size": "2714",
"hash": "4f5d185b0c671e6165902762681f098b"
},
"three":
{
"name": "thumbnail.aspx.jpeg",
"time": "1260393951",
"size": "5268",
"hash": "6939efa58ff7ffcac17218ffdd0d5d8c"
}
}
Then you reference your fields like this when you have the object:
myArray["two"]["name"]
myArray["two"].name
myArray.two.name
Your returned file isn't JSON. Unless you're using PHP syntax to describe your JSON object for us, you need to encode it into JSON format using json_encode.
What you call the JSON file isn't JSON. Or maybe do you use some PHP library that converts that strange format into JSON ?

Categories