i cant for the life of me figure out why i cant get this JSON to work
this is how it comes back from api
"{"errorCode":5,"errorDescription":"Unknown account"}"
i have tried all of the following to get "Unknown account"
$error = $result['errorCode":5,"errorDescription'];
$error = $result['errorCode'];
$error = $result['errorDescription'];
i would be ok even just trying for error code 5, so i can do a check to further the script if account is made or not.
any ideas what i am missing?
try this:
$json = '{"errorCode":5,"errorDescription":"Unknown account"}';
$arr = json_decode($json, true);
$error = $arr['errorDescription'];
In this case you take the json and parse it into the function json_decode of php, wit true value as second parameter It return an associative error where you can get your value by key.
DEMO
It's actually a simple matter as there are already builtin functions in PHP for handling JSON. Those are json_encode and json_decode.
$result='{"errorCode":5,"errorDescription":"Unknown account"}';
var_dump($result);
$json=json_decode($result);
var_dump($json);
echo "errorCode={$json->errorCode}<br>";
echo "errorDescription={$json->errorDescription}<br>";
Try the above. Note that json_decode can convert a string into either an object (default) or an associative array.
$json2=json_decode($result,TRUE);
var_dump($json2);
echo "errorCode=".$json2['errorCode']."<br>";
echo "errorDescription=".$json2['errorDescription']."<br>";
Related
For some strange reason I can't change the following JSON to a PHP array:
{"sides0":{"name_nl":"Voorkant100","name":"Frontside100","template_overlay":""},"sides1":{"name_nl":"Achterkant100","name":"Backside100","template_overlay":"1"}}
I've validated the json and it's valid.
First I post $product['sides'] to my page, containing:
"{\"sides0\":{\"name_nl\":\"Voorkant100\",\"name\":\"Frontside100\",\"template_overlay\":\"\"},\"sides1\":{\"name_nl\":\"Achterkant100\",\"name\":\"Backside100\",\"template_overlay\":\"1\"}}"
Then I use json_decode on it like this:
$sidearr = json_decode($product['sides'], true);
If I then do:
echo '<pre>';
print_r($sidearr);
echo '</pre>';
It prints the first part of my question.
Now I want to loop over it, but even this test shows nothing:
foreach($sidearr as $side){
echo 'test';
}
I tried testing if it even is an array with:
echo is_array($sidearr) ? 'Array' : 'not an Array';
echo "\n";
And it shows me that it is not an array. Why is that? I always thought using json_decode on a json string and add true inside the function turns it into a PHP array.
It prints the first part of my question.
Because $sidearr is a string now, decode it again, you'll get an array.
$sidearr = json_decode($sidearr, true);
<?php
function get_stuff()
{
($_GET['http://****RemovedForSecurityPurposes****.repairshopr.com/api/v1/tickets?api_key=****RemovedForSecurityPurposes****']);
echo $_GET;
}
get_stuff();
?> /* Yes, Pointless closing tag, I'm cool like that. */
For some reason when I run this code the output I'm getting is "Array" and I can't figure out why? I know it is an array that I'm getting from the URL but I thought it would just print in whatever format its in? Am I missing something?
Thanks In advance!
You cannot pass website URL to $_GET.
You should use file_get_contents()
$content = file_get_contents('http://YOUR_URL');
If you have valid json then you can convert it to an array.
$data = json_decode($content, true);
then print it,
echo '<pre>'; print_r($data);
The Message of OP when I run this code the output I'm getting is
"Array".
You need to use file_get_contents to read a file from a remote server. If your file response an array then you have to use print_r or var_export or var_dump. Or if your file response is a string(json) then you need to store it in a variable and apply a decode method.
function get_stuff(){
$response = file_get_contents('http://****RemovedForSecurityPurposes****.repairshopr.com/api/v1/tickets?api_key=****RemovedForSecurityPurposes****');
print_r($response); // if array
$arr = json_decode($response); // decode of json
print_r($arr);
}
get_stuff();
I think you will understand what i mean. Let me know if you are useful or need some help.
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 have a string like this:
{
"update_id":659510742,
"message":{
"message_id":178,
"from":{
"id":110910409,
"first_name":"S.M_Emamian"
},
"chat":{
"id":-57184742,
"title":"Vvggg",
"type":"group"
},
"date":1446970836,
"new_chat_participant":{
"id":131677747,
"first_name":"Shadyab",
"username":"Shadyabbot"
}
}
}
now, I would like to get first_name. how can I get that ?
I tested below code, but it doesn't work :
$json_a = json_decode($content, true);
$first_name = $json_a->message->from->first_name; //it returns nothing.
$json_a = json_decode($content, true);
^^^^
That true forces the output of json_decode to be an array, but you are trying to treat it like an object. Remove that argument or use array syntax:
$first_name = $json_a['message']['from']['first_name'];
use only json_decode($content) remove true
try this code :-
$json_a = json_decode($content);
echo $json_a->message->from->first_name;
True convert the output of json_decode to be an array
$json_a = json_decode($content, true);
$first_name = $json_a->message->from->first_name; //it returns nothing.
That's because the function returns an associative array, and not an object.
By using -> you're trying to access object properties of $json_a. What you're really looking for is:
$json_a["message"]["from"]["first_name"]
This way you're accessing the associative array values by their key, and not trying to access object properties.
I hope this helped,
Sebastian
$json_a = json_decode($content);
$first_name = $json_a->message->from->first_name;
or
$json_a = json_decode($content,true);
$first_name = $json_a[message][from][first_name];
You're passing true as the second argument to json_decode which tells it to return an associative array rather than an object.
Either remove the second argument (or change it to the default, false), or access the fields using array syntax:
$first_name = $json['message']['from']['first_name'];
json_decode() is a php function used to decode json into either php standard object or an associative array dependent upon argument passed to it
When true it will return an associative otherwise by default it returns standard PHP object
According to your requirement please try executing following code snippet for retrieving first name from json document.
$json_a = json_decode($content, true);
$first_name = $json_a['message']['from']['first_name'];
For more detailed description regarding parsing json documents in php please refer the documentation in following URL
http://php.net/manual/en/function.json-decode.php
I'm trying to decode the following JSON using php json_decode function.
[{"total_count":17}]
I think the square brackets in output is preventing it. How do I get around that? I can't control the output because it is coming from Facebook FQL query:
https://api.facebook.com/method/fql.query?format=json&query=SELECT%20total_count%20FROM%20link_stat%20WHERE%20url=%22http://www.apple.com%22
PHP's json_decode returns an instance of stdClass by default.
For you, it's probably easier to deal with array. You can force PHP to return arrays, as a second parameter to json_decode:
$var = json_decode('[{"total_count":17}]', true);
After that, you can access the variable as $result[0]['total_count']
See this JS fiddle for an example of how to read it:
http://jsfiddle.net/8V4qP/1
It's basically the same code for PHP, except you need to pass true as your second argument to json_decode to tell php you want to use it as associative arrays instead of actual objects:
<?php
$result = json_decode('[{"total_count":17}]', true);
print $result[0]['total_count'];
?>
if you don't pass true, you would have to access it like this: $result[0]->total_count because it is an array containing an object, not an array containing an array.
$json = "[{\"total_count\":17}]";
$arr = Jason_decode($json);
foreach ($arr as $obj) {
echo $obj->total_count . "<br>";
}
Or use json_decode($json, true) if you want associative arrays instead of objects.