json decode changing the value - php

I have below json array.
{"entries":[{"uid":155551338258538,"photo":"https:\/\/m.ak.fbcdn.net\/profile.ak\/hprofile-ak-prn1\/323887_155551338258538_1152153357_q.jpg","type":"user","text":"shikhadamodar","path":"\/shikha.damodar.1","category":"Madiun","names":["shikhadamodar"],"index_rank":-1,"needs_update":true,"non_title_tokens":"madiun"},{"uid":155551332173428,"photo":"https:\/\/m.ak.fbcdn.net\/profile.ak\/hprofile-ak-snc3\/273318_155551332173428_2131571532_q.jpg","type":"user","text":"shreya damodar","path":"\/my.devprofile","category":"Nilai","names":["shreyadamodar"],"index_rank":-1,"needs_update":true,"non_title_tokens":"nilai"},{"uid":155555587232111,"photo":"https:\/\/m.ak.fbcdn.net\/profile.ak\/hprofile-ak-prn1\/41332_155555587232111_3158_q.jpg","type":"user","text":"neha neha","path": "\/neha.neha34","category":"New York, New York","names":["Neha neha"],"index_rank":-1,"needs_update":true,"non_title_tokens":"new york, new york"}],"token":"1333551453-7","display_token":5,"display_ttl":1333541132}
when i decode this json array, i am getting ,
array(4){["entries"]=>array(3){[0]=>array(10){["uid"]=>float(1.5555133825854E+14)["photo"]=>string(90) "https://m.ak.fbcdn.net/profile.ak/hprofile-ak-prn1/323887_155551338258538_1152153357_q.jpg"["type"]=>string(4) "user"["text"]=>string(13) "shikhadamodar"["path"]=>string(17) "/shikha.damodar.1"["category"]=>string(6) "Madiun"["names"]=>array(1){[0]=>string(13) "shikhadamodar"}["index_rank"]=>int(-1)["needs_update"]=>bool(true)["non_title_tokens"]=>string(6) "madiun"}[1]=>array(10){["uid"]=>float(1.5555133217343E+14)["photo"]=>string(90) "https://m.ak.fbcdn.net/profile.ak/hprofile-ak-snc3/273318_155551332173428_2131571532_q.jpg"["type"]=>string(4) "user"["text"]=>string(14) "shreya damodar"["path"]=>string(14) "/my.devprofile"["category"]=>string(5) "Nilai"["names"]=>array(1){[0]=>string(13) "shreyadamodar"}["index_rank"]=>int(-1)["needs_update"]=>bool(true)["non_title_tokens"]=>string(5) "nilai"}[2]=>array(10){["uid"]=>float(1.5555558723211E+14)["photo"]=>string(83) "https://m.ak.fbcdn.net/profile.ak/hprofile-ak-prn1/41332_155555587232111_3158_q.jpg"["type"]=>string(4) "user"["text"]=>string(9) "neha neha"["path"]=>string(12) "/neha.neha34"["category"]=>string(18) "New York,New York"["names"]=>array(1){[0]=>string(9) "Neha neha"}["index_rank"]=>int(-1)["needs_update"]=>bool(true)["non_title_tokens"]=>string(18) "new york,new york"}}["token"]=>string(12) "1333551453-7"["display_token"]=>int(5)["display_ttl"]=>int(1333541132)}
first UID in json is "uid":155551338258538,
when it comes to array, it becomes ["uid"]=>float(1.5555133825854E+14)
why is it so??
After getting 1.5555133825854E+14 a a reult, ho can i change that to real UID 155551338258538
?
Hope some great coder can help on this.

I found solution of your problem the UID string is long intger hence json_decode is converting it to scientific notation. Try this
print_r(json_decode($arr, false,512, JSON_BIGINT_AS_STRING));

Related

JSON_decode() return NULL

I am trying to convert JSON plain text to php array but it always returns NULL. Actually, I am building a website which fetches the quran from other website through API. And the text type is ARABIC (utf_8).
Here's my JSON:
{"code":200,"status":"OK","data":[{"identifier":"ur.ahmedali","language":"ur","name":"\u0627\u062d\u0645\u062f \u0639\u0644\u06cc","englishName":"Ahmed Ali","format":"text","type":"translation"},{"identifier":"ur.jalandhry","language":"ur","name":"\u062c\u0627\u0644\u0646\u062f\u06c1\u0631\u06cc","englishName":"Fateh Muhammad Jalandhry","format":"text","type":"translation"},{"identifier":"ur.jawadi","language":"ur","name":"\u0639\u0644\u0627\u0645\u06c1 \u062c\u0648\u0627\u062f\u06cc","englishName":"Syed Zeeshan Haider Jawadi","format":"text","type":"translation"},{"identifier":"ur.kanzuliman","language":"ur","name":"\u0627\u062d\u0645\u062f \u0631\u0636\u0627 \u062e\u0627\u0646","englishName":"Ahmed Raza Khan","format":"text","type":"translation"},{"identifier":"ur.qadri","language":"ur","name":"\u0637\u0627\u06c1\u0631 \u0627\u0644\u0642\u0627\u062f\u0631\u06cc","englishName":"Tahir ul Qadri","format":"text","type":"translation"},{"identifier":"ur.junagarhi","language":"ur","name":"\u0645\u062d\u0645\u062f \u062c\u0648\u0646\u0627\u06af\u0691\u06be\u06cc","englishName":"Muhammad Junagarhi","format":"text","type":"translation"},{"identifier":"ur.maududi","language":"ur","name":"\u0627\u0628\u0648\u0627\u0644\u0627\u0639\u0644\u06cc \u0645\u0648\u062f\u0648\u062f\u06cc","englishName":"Abul A'ala Maududi","format":"text","type":"translation"},{"identifier":"ur.khan","language":"ur","name":"Shamshad Ali Khan","englishName":"Shamshad Ali Khan","format":"audio","type":"versebyverse"}]}
I think the problem is because of Arabic characters.
I have tried doing:
utf_unicode(json_decode($str, true));
stripcslashes(json_decode($str, true));
But it never worked
I have everything from other questions similar to mine.
Thanks
You are passing in the wrong parameters to utf_unicode() and stripcslashes(), both of these take a string not an array, json_decode() (in your case) is intended to return an array.
In order to use this JSON string in PHP directly, use PHP's NOWDOC or HEREDOC syntax like below:
Please try:
$str = <<< JSON
{"code":200,"status":"OK","data":[{"identifier":"ur.ahmedali","language":"ur","name":"\u0627\u062d\u0645\u062f \u0639\u0644\u06cc","englishName":"Ahmed Ali","format":"text","type":"translation"},{"identifier":"ur.jalandhry","language":"ur","name":"\u062c\u0627\u0644\u0646\u062f\u06c1\u0631\u06cc","englishName":"Fateh Muhammad Jalandhry","format":"text","type":"translation"},{"identifier":"ur.jawadi","language":"ur","name":"\u0639\u0644\u0627\u0645\u06c1 \u062c\u0648\u0627\u062f\u06cc","englishName":"Syed Zeeshan Haider Jawadi","format":"text","type":"translation"},{"identifier":"ur.kanzuliman","language":"ur","name":"\u0627\u062d\u0645\u062f \u0631\u0636\u0627 \u062e\u0627\u0646","englishName":"Ahmed Raza Khan","format":"text","type":"translation"},{"identifier":"ur.qadri","language":"ur","name":"\u0637\u0627\u06c1\u0631 \u0627\u0644\u0642\u0627\u062f\u0631\u06cc","englishName":"Tahir ul Qadri","format":"text","type":"translation"},{"identifier":"ur.junagarhi","language":"ur","name":"\u0645\u062d\u0645\u062f \u062c\u0648\u0646\u0627\u06af\u0691\u06be\u06cc","englishName":"Muhammad Junagarhi","format":"text","type":"translation"},{"identifier":"ur.maududi","language":"ur","name":"\u0627\u0628\u0648\u0627\u0644\u0627\u0639\u0644\u06cc \u0645\u0648\u062f\u0648\u062f\u06cc","englishName":"Abul A'ala Maududi","format":"text","type":"translation"},{"identifier":"ur.khan","language":"ur","name":"Shamshad Ali Khan","englishName":"Shamshad Ali Khan","format":"audio","type":"versebyverse"}]}
JSON;
$arr = json_decode($str, true);
print_r($arr);
Hope this helps,

php decode JSON get values

I'm trying to decode JSON format
What I am sending is:
{
"id": 123,
"name": "John",
“surname”: “Smith”,
“department”: 3
}
I am sending POST with data via Postman, in the picture.
So, this is the data I want to decode:
"data_serever_received": "{ \"id\": 123, \"name\": \"john\", “surname”: “Smith”, “department”: 3 }"
I tried
$this->input->data["id"]
but it is not working.
How can I get the id, name, surname and etc. values?
Your JSON is invalid “ and ” are not ".
(Zoom in with your browser if you can't see the difference).
You need to start with valid JSON before you can parse it.
You can use json_decode to make it an array.
$json_array = json_decode($json_object);//$json_object will need to contain your json string.
then you can access like this:
echo $json_array["data"][“surname”];
PHP Doc on json_decode: http://php.net/manual/en/function.json-decode.php

PHP JSON Google Definitions - accessing a value

EDIT#4: json_decode is failing and returning null on a seemingly valid json string. See below for more info
I am new to JSON/JSONP and I'm running into constant trouble accessing the values in the returned JSON with PHP. I have stripped the JSONP callback without issue using code I found on this board. I am getting a JSONP result from http://www.google.com/dictionary/json?callback=a&sl=en&tl=en&q=love and struggling to access the first result for the meaning. It's a quite complex result, and I need to access the first meaning (in the node "text") from the below JSON result.
http://pastebin.com/hBTeBTUL
My best attempt was:
if (isset($json->primaries[1]->entries[1]->terms[1]->text))
The above was the best I could do, I just keep getting errors trying to return that text node saying it is undefined. I'd prefer to work with objects rather than associative arrays too, if possible, so please avoid telling me to set it to return assoc array.
Any help would be greatly appreciated. I'm really stuck :P
EDIT:
$json->primaries[1]->entries[1]->terms[0]->text didn't seem to work either. Here is the complete script. Ignore the $params array as it is not used, was going to use it to generate the query.
The script has been edited since when I first posted, I had an invalid JSON object, but the error seems to be fixed as it will now parse through JSON formatters.
The error i'm getting trying to print the value out is
PHP Notice: Trying to get property of non-object in /home/outil2/Plugins/GDefine.php on line 23
EDIT#2: added json_decode which was in my original solution, but got lost in the second version
<?php
class GDefine extends Plugin {
public static $enabled = TRUE;
public function onReceivedData($data) {
if ($data["message"][0] == ".def") {
$params = array (
"callback" => "a",
"sl" => "en",
"tl" => "en",
"q" => $data["message"][1]
);
$jsonp = file_get_contents(
"http://www.google.com/dictionary/json?callback=a&sl=en&tl=en&q=" . $data["message"][1]);
$json = json_decode(substr($jsonp, 2, strlen($jsonp)-12));
var_dump($json);
print_r($json->primaries[1]->entries[1]->terms[0]->text);
if (isset($json->primaries[1]->entries[1]->terms[0]->text)) {
$text = $this->bold("Google Definition: ");
$text .= $this->teal($json->primaries[1]->entries[1]->terms[0]->text);
$this->privmsg($data["target"], $text);
} else {
$this->privmsg($data["target"], "error error error");
}
}
}
}
EDIT #3: this is the string I'm trying to json_decode, after using substr to remove the callback function, but am getting a NULL value returned on var_dump($json)
{"query":"love","sourceLanguage":"en","targetLanguage":"en","primaries":[{"type":"headword","terms":[{"type":"text","text":"love","language":"en","labels":[{"text":"Noun","title":"Part-of-speech"}]},{"type":"phonetic","text":"/lÉv/","language":"und"},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/love.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"loves","language":"und","labels":[{"text":"plural"}]}]},{"type":"meaning","terms":[{"type":"text","text":"An intense feeling of deep affection","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"babies fill parents with intense feelings of \x3cem\x3elove\x3c/em\x3e","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"their \x3cb\x3e\x3cem\x3elove\x3c/em\x3e for\x3c/b\x3e their country","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A deep romantic or sexual attachment to someone","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"it was \x3cem\x3elove\x3c/em\x3e at first sight","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"they were both \x3cb\x3ein \x3cem\x3elove\x3c/em\x3e with\x3c/b\x3e her","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"we were slowly \x3cb\x3efalling in \x3cem\x3elove\x3c/em\x3e\x3c/b\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A personified figure of \x3cem\x3elove\x3c/em\x3e, often represented as Cupid","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"A great interest and pleasure in something","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"his \x3cb\x3e\x3cem\x3elove\x3c/em\x3e for\x3c/b\x3e football","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"we share a \x3cb\x3e\x3cem\x3elove\x3c/em\x3e of\x3c/b\x3e music","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Affectionate greetings conveyed to someone on one\x27s behalf","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"A formula for ending an affectionate letter","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"take care, lots of \x3cem\x3elove\x3c/em\x3e, Judy","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A person or thing that one \x3cem\x3eloves\x3c/em\x3e","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"she was \x3cb\x3ethe \x3cem\x3elove\x3c/em\x3e of his life\x3c/b\x3e","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"their two great \x3cem\x3eloves\x3c/em\x3e are tobacco and whiskey","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A friendly form of address","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"it\x27s all right, \x3cem\x3elove\x3c/em\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Used to express affectionate approval for someone","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"don\x27t fret, there\x27s a \x3cem\x3elove\x3c/em\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"(in tennis, squash, and some other sports) A score of zero; nil","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"\x3cem\x3elove\x3c/em\x3e fifteen","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"he was down two sets to \x3cem\x3elove\x3c/em\x3e","language":"en"}]}]}]},{"type":"headword","terms":[{"type":"text","text":"love","language":"en","labels":[{"text":"Verb","title":"Part-of-speech"}]},{"type":"phonetic","text":"/lÉv/","language":"und"},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/love.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"loved","language":"und","labels":[{"text":"past participle"}]},{"type":"text","text":"loves","language":"und","labels":[{"text":"3rd person singular present"}]},{"type":"text","text":"loving","language":"und","labels":[{"text":"present participle"}]},{"type":"text","text":"loved","language":"und","labels":[{"text":"past tense"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Feel a deep romantic or sexual attachment to (someone)","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"do you \x3cem\x3elove\x3c/em\x3e me?","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Like very much; find pleasure in","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"I\x27d \x3cem\x3elove\x3c/em\x3e a cup of tea, thanks","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"I just \x3cem\x3elove\x3c/em\x3e dancing","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"a fun-\x3cem\x3eloving\x3c/em\x3e girl","language":"en"}]}]}]}]}
I json_decode that and it returns NULL :(
You're trying to access an object that doesn't exist. Your code:
if (isset($json->primaries[1]->entries[1]->terms[1]->text)) // Doesn't exist
There's no terms[1] in entries[1] in primaries[1]. There's just 1 item; terms[0]. I think this will work for example:
if (isset($json->primaries[1]->entries[1]->terms[0]->text))
The first item in the array is indexed by 0 not 1, maybe that's your mistake.
Edit:
You also need to decode the JSON, change:
$json = substr($jsonp, 2, strlen($jsonp)-12);
to:
$json = json_decode(substr($jsonp, 2, strlen($jsonp)-12));
Edit:
You need to escape some unescaped characters in the JSON as well. Add this to your code:
Change:
$json = json_decode(substr($jsonp, 2, strlen($jsonp)-12));
to:
$json = substr($jsonp, 2, strlen($jsonp) - 12);
$json = str_replace("\\", "\\\\", $json);
$json = json_decode($json);

Empty string returned in Google Reverse geocoding webservice JSON

I have tried to use Reverse geocoding Webservice in PHP.Response JSON returns Empty response only.
$json_string2='http://maps.googleapis.com/maps/api/geocode/json?latlng=11.49813514,77.24331624&sensor=false';
$obj2=json_decode($json_string2);
$addr2=$obj2->formatted_address;
echo $addr2; //**line 1**
here line 1 prints empty....What is the problem in the coding....
Your problem is you did not get the file before decode to JSON and you also called with the wrong format.
$json_string2 = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=11.49813514,77.24331624&sensor=false');
$obj2 = json_decode($json_string2);
$addr2 = $obj2->results[0]->formatted_address;
echo $addr2; //**line 1**
you could try to print this object before you call its format.
print_r($obj2);

Extract Data from a Website using PHP

I am trying to get PHP to extract the TOKEN (the uppercase one), USERID (uppercase), and the USER NAME (uppercase) from a web page with the following text.
{
"rsp":{
"stat":"ok",
"auth":{
"token":"**TOKEN**",
"perms":"read",
"user":{
"id":"**USERID**",
"username":"**USER NAME**",
"fullname":"**NAME OF USER**"
}
}
}
}
(This is from the RTM api, getting the authentication token of the user).
How would I go about doing this? Thanks!
EDIT:
how would i get task name "Buy Milk!" & the due date of the task"2011-02-28T.." using json_decode and php here? Thanks!
{
"rsp":{
"stat":"ok",
"tasks":{
"rev":"REV_NUMBER",
"list":{
"id":"ID_NUMBER",
"taskse­­ries":{
"id":"ID_NUMBER",
"created":"2010-11-16T00:01:50Z",
"modified":"2011-02-28T05:09:36Z",
"name":"Buy Milk!",
"source":"js",
"url":"",
"location_id":"",
"rrule":{
"every":"1",
"$t":"FREQ=W­­EEKLY;INTERVAL=1"
},
"tags":[
],
"participants":[
],
"notes":[
],
"task":{
"id":"ID_NUMBER" ­­,
"due":"2011-02-28T05:00:00Z",
"has_due_time":"0",
"added":"2011-02-21T05:04:49Z",
"completed":"",
"deleted":"",
"priority":"2",
"postponed":"0",
"estima­­te":""
}
}
}
}
}
}
As Delan suggested, use json_decode. Here is an example of how you would use json_decode to extract the information you require.
// your json string
$string = '{"rsp":{"stat":"ok","auth":{"token":"**TOKEN**","perms":"read","user":{"id":"**USERID**","username":"**USER NAME**","fullname":"**NAME OF USER**"}}}}';
// parse json string to an array
$array = json_decode($string, true);
// auth token
echo $array['rsp']['auth']['token'];
// user details
echo $array['rsp']['auth']['user']['id'];
echo $array['rsp']['auth']['user']['username'];
echo $array['rsp']['auth']['user']['fullname'];
UPDATE I've updated the code to use json_decode's $assoc parameter to convert from an object to an assoc array.
ANOTHER UPDATE To answer your updated question..
how would i get task name "Buy Milk!" & the due date of the task"2011-02-28T.." using json_decode and php here? Thanks!
This code would work to get the values that you want.
//string(9) "Buy Milk!"
echo $array['rsp']['tasks']['list']['taskseries']['name'];
// string(20) "2011-02-28T05:00:00Z"
echo $array['rsp']['tasks']['list']['taskseries']['task']['due'];
This code isn't ideal, but it gets the job done for you.
If the string is JSON, json_decode in PHP will do the trick. It's was implemented in PHP 5.2.0.
http://www.php.net/manual/en/function.json-decode.php

Categories