As far as i call recall. A valid Json data comes in this format '{"key":"value"}'
But while surfing, I found an article about sending UTF-8 codes as emoji.
Emoji was stored in variable as
$emoji = "\ud83d\udc4e";. For it to work properly, the answer was to use json_decode($emoji);. I tried it out and it returned a thumbs down emoji. Meanwhile, I was expecting NULL but it turns out that it was a valid json data. So I'm confused how that is possible.
My API URL Returned code in browser as shown below. but json_decode($api_url,true); returns null.
i checked json_last_error();, it returns 4(json error syntax).
it worked with json_decode(file_get_contents($api_url),true);
why it isn't work with json_decode. please help
{"dataset":{"id":27153572,"dataset_code":"20MICRONS_A_DEBT","database_code":"DEB","name":"20 Microns Limited,Total Debt","description":"\u003cp\u003e20 Microns Limited(NSE:20MICRONS)-Total Debt(Annual)\u003c/p\u003e","refreshed_at":"2018-09-21T08:04:08.278Z","newest_available_date":"2018-03-31","oldest_available_date":"2005-03-31","column_names":["PERIOD","STANDALONE","CONSOLIDATED"],"frequency":"annual","type":"Time Series","premium":true,"limit":null,"transform":null,"column_index":null,"start_date":"2005-03-31","end_date":"2018-03-31","data":[["2018-03-31",128.56,133.68],["2017-03-31",144.9,151.73],["2016-03-31",155.18,163.41],["2015-03-31",152.8,164.62],["2014-03-31",162.01,176.64],["2013-03-31",148.49,164.73],["2012-03-31",144.67,158.6],["2011-03-31",81.42,120.31],["2010-03-31",84.35,87.35],["2009-03-31",58.62,58.62],["2008-03-31",46.52,null],["2007-03-31",42.46,null],["2006-03-31",40.03,null],["2005-03-31",38.98,null]],"collapse":null,"order":null,"database_id":14992}}
What you are trying to do has no sense. $api_url is just an url so when you try to decode it then it doesn't have a json stucture and it will throw an exeption.
What you should decode is the data that this url returns to you.
So First you should get data from url then use json_decode($api_url,true);.
To get data you can use file_get_contents or curl.
Props to u_mulder who hit the nail on the head but I want to break this down for you a little.
Purpose of file_get_contents():
The file_get_contents() function is an inbuilt function in PHP which is used to read the contents of a file into a string.
Please note that the 'file' can be a file residing on your web server or a remote file (URL), which in essence will give you a web document back.
Purpose of json_decode():
The json_decode() function is an inbuilt function in PHP which is used to decode a JSON string. It converts a JSON encoded string into a PHP variable.
With that in mind you can see that performing json_decode on an invalid JSON string or a URL will render your result as NULL.
json_decode('https://www.website.com')
This is essentially what you are doing. performing the file_get_contents inside your json_decode first converts your URL/File ('https://www.website.com') into a string, that string then having JSON is then converted into an array by json_decode.
I am trying to sync messages of android device to server using Java script object notation, i am able to sync all of the columns e.g. address, date, status, read, type except message body, it gives Java script object notation syntax error. i have gone through many links, they are saying that there are some sort of characters/hidden characters, i tried their solutions but doesn't helped at all, i tried some of these:
json_decode returns JSON_ERROR_SYNTAX but online formatter says the JSON is OK
json_decode syntax error from valid json
I am trying to encode android sms message body using JSON and send over the php webservice, but on php end it was not interpreted as JSON because of hidden chracters inside the string of message body. Adding this one line of code to replace hidden characters in string on Android end solves the problem, here's the solution: body.replaceAll("\p{C}", "?");
Here is a string sample that I get from my DB which is maintained in php. I am not sure as how to use it in my iPhone as to extract various values:
a:27:{s:26:"the-muse_disable_post_meta";s:5:"false";s:31:"the- muse_disable_published_date";s:4:"true";s:23:"the-muse_disable_author";s:5:"false";s:24:"the-muse_disable_coments";s:5:"false";s:19:"the-muse_page_title";s:13:"default_title";s:21:"the-muse_custom_title";s:0:"";
Change the response to a JSON Array and you can simply parse it in every coding language.
PHP
echo json_encode($array);
i'm facing a problem during a parse of a JSON string in my android application.
I'm trying to receive some data from my database located on my website. The php script which execute this request encode the data queried in database in a JSON string. An example of this string is :
{"file0":["name":"intro.wmv","path":"C:\\Temp","bla":"0"],"file1":["name":"video.avi","path":"C:\\Temp","bla":"0"]}
This file is an array of elements of type FILE which has some properties like the "filename", "folder path".
Once i receive this string in my android application, when i try to parse it using GSON :
Type listType = new TypeToken<ArrayList<MyFiles>>() {}.getType();
List<MyFiles> yourClassList = new Gson().fromJson(sResponse, listType);
it throws an exception. Something wrong happens with the "folder path" property, because if i remove it from the JSON string, all goes well!
Probably the issue is due to the backslash char '\' ...
Someone faced this issue before?? Have you any hint to resolve this problem ???
Thanks in advance
Paolo
I don't see how ["intro.wmv","C:\\Temp","0"] could match MyFile object. You should specify the field names rather than values in a array.
For example {"fileName":"intro.wmv","path","C:\\Temp","size":"0"}