Can't parse single letter and single number json response - php

I've an issue with a Json Response in Android. The json I'm getting is:
"latlng": [
"s:78:\"a:2:{s:3:\"lat\";s:18:\"40.653198478383175\";s:3:\"lng\";s:18:\"-73.95414308015137\";}\";"
],
and I should be getting something like this:
"latlng": {
"address": "8, Bolivia",
"lat": "-10.92968864676556",
"lng": "-65.7861328125"
},
And looks like a big string with some "supposed" json objects with single letters and single numbers.
I can't find the way to parse this information with the most used json parsers for android.
I'm using the UTF-8 in Android when trying to parse it, but not change at all.
This is an issue server side? My client is using a wordpress site with WP JSON REST API but this info comes from a Theme with custom meta values and meta keys.
How could I parse this info in Android? Thanks in advance!
EDIT:
Please, check the full json response here: http://pastebin.com/Xm7w3UKG and don't post me "that not a json response" because it's a valid json response.

Related

how to handle JSON embedded quotes in title with youtube-data-api-v3

I have been getting JSON decode errors with data returned by YouTube Data API v3, where the returned JSON includes quotes embedded within the title field.
$json = curl_init();
$optArray = array(
CURLOPT_URL => $apiCall,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE
);
curl_setopt_array($json, $optArray);
$result = curl_exec($json);
curl_close($json);
Error Message: Syntax error
A recent example includes "videoId": "Q6AYg0oLfC8", where this is returned for the title field: "title": "Trump and His Allies Want You to "Adapt" to the Coronavirus Crisis: A Closer Look".
With the PHP 7 JSON decoder v 1.4.0, this JSON does not parse due to the embedded quotes in the title field.
What is the best practice to handle embedded quotes in title or any other field returned by API JSON responses?
I can upload the JSON returned by YouTube as a file if that helps. (Or can explain how files are attached to Stack Overflow.)
In my experience, I never came across invalid JSON produced by any of the endpoints of YouTube's Data API.
On the other hand, please note that embedding quotes in strings is perfectly legal JSON. That is, if obeying the prescribed syntax: each quotation mark character to be embedded into a JSON string must be escaped, i.e. must be preceded by a single reverse solidus character -- that is \.
Otherwise, this basic feature of JSON is handled seamlessly by json_decode. (Am assuming that you actually are using json_decode.) For example, in case of this simple JSON text:
{"foo":"bar\"baz\"boo"}
json_decode works OK according to this site. Note that in PHP, that JSON text and the call to that function would look like:
$val = json_decode('{"foo":"bar\\"baz\\"boo"}')
For to try helping you further, do post here the relevant piece of PHP code that produces the error mentioned, along with the error message(s) generated.
Update upon OP's posting source code
Upon running the following curl command line at a stock GNU/Linux bash prompt:
$ curl -o Q6AYg0oLfC8.json "https://www.googleapis.com/youtube/v3/videos?key=$YOUTUBE_DATA_APP_KEY&id=Q6AYg0oLfC8&part=contentDetails,id,liveStreamingDetails,player,recordingDetails,snippet,statistics,status,topicDetails"
I did obtained a valid JSON text. The title field looks like shown below:
$ grep Q6AYg0oLfC8.json -ne '"title"' -B4
8- "id": "Q6AYg0oLfC8",
9- "snippet": {
10- "publishedAt": "2020-07-16T01:00:02Z",
11- "channelId": "UCVTyTA7-g9nopHeHbeuvpRA",
12: "title": "Trump and His Allies Want You to \"Adapt\" to the Coronavirus Crisis: A Closer Look",
--
91- ],
92- "categoryId": "23",
93- "liveBroadcastContent": "none",
94- "localized": {
95: "title": "Trump and His Allies Want You to \"Adapt\" to the Coronavirus Crisis: A Closer Look",
These strings are indeed valid.

Combine multiple streamed JSON objects into a single JSON object - remove first returned object

I've seen the answers to similar questions - but they do not solve the issue I'm having. So here goes..
I have a response from a MailChimp EXPORT API that looks like this:
["Email Address","First Name","Last Name","Source","Open","MEMBER_RATING","OPTIN_TIME","OPTIN_IP","CONFIRM_TIME","CONFIRM_IP","LATITUDE","LONGITUDE","GMTOFF","DSTOFF","TIMEZONE","CC","REGION","LAST_CHANGED","LEID","EUID","NOTES"]
["email1#somewhere.com","","","BORN","",2,"",null,"2015-05-07 15:25:48",null,null,null,null,null,null,null,null,"2015-05-07 15:25:48","74554133","ar5jiewpkc",null]
["email2#somwhere.com","","","BORN","",2,"",null,"2015-05-07 15:25:48",null,null,null,null,null,null,null,null,"2015-05-07 15:25:48","74554137","ar5jiewpkc",null]
What I need to do is two things:
Remove the first stream object (containing "Email Address" .. etc)
Combine the rest of the objects into a single JSON response to pass to a script as if it were a single regular JSON response.
This is a JSON "Stream" that I need to turn into a regular JSON response.
I've successfully turned them all into arrays, but when I try to json_encode the array of arrays, I get back escaped data that is not a true JSON object.
Any help is appreciated!

Sencha Touch 2.x [WARN][Ext.data.reader.Reader#process] JSON object not found

i am trying to get data from mysql as json to a nested list in Sencha Touch.
Only Problem is i get this Error and dont know why:
[WARN][Ext.data.reader.Reader#process] Unable to parse the JSON returned by the server:
Error: You're trying to decode an invalid JSON String: data=[ "name":rezepte{"id":"1","name":null,"alter":"Ab 6.Monat","kategorie":null,"tageszeit":"Mittags","portionen":"1 baby- und 1 Erwachsenenportion","bild":"\u0000\u0000\u0000\u0000\u0000","vorrat":"2",
"zubereitungszeit":"45","zubereitung0":null,"zubereitung1":null,"zubereitung2":null,"info":null}]
[WARN][Ext.data.reader.Reader#process] JSON object not found
fields containing utf-8 signs appear as null. DB is utf-8_general so i thought it might be the way the json returned looks. switch around but didnt work.
Please let me know what you need to help me.
Your JSON is invalid or your server responding with invalid JSON.
It should be like this
{
"name":"rezepte",
"id":"1",
"name":null,
"alter":"Ab 6.Monat",
"kategorie":null,
"tageszeit":"Mittags",
"portionen":"1 baby- und 1 Erwachsenenportion",
"bild":"\u0000\u0000\u0000\u0000\u0000",
"vorrat":"2",
"zubereitungszeit":"45",
"zubereitung0":null,
"zubereitung1":null,
"zubereitung2":null,
"info":null
}

Quote in json data

Hi you can see there's quote before rural management, this is causing problem while json decoding.. how can i retrive this using PHP json_decode?
[{
"employ":{
"id":"234",
"name":""rural management"
},
"pos":{
"name":"Programme Officer"
}
}]
You can't. The data is not JSON.
You need to make sure the data is in JSON format before passing it to json_decode.
As a quick hack, you could just perform a string substitution and change "" to "\" globally, but that isn't a robust solution. There is something wrong with the underlying JSON generator that you are getting the data from and that needs to be fixed at the source (probably by switching to using a JSON library instead of smashing strings together).

Cakephp JSON response with json_encode not working

I'm using ajax function to get the data back for my jquery autocomplete but it seems to not parse the json response and I cannot find why.
I look did console.log for one that is working which is another json response and the other one which is not working and in chrome Console, I can see below. The first one is not working and second one is working.
["17","17","16","20","19","18","23","18","20","18","23","23"]
["25", "24", "25", "24", "24", "23", "21", "23", "22", "21", "22", "22"]
I can see that the second one has red color on numbers and I cannot find out why.
Can someone find out why it's not parsing this JSON?
The second one has a space (or some other character that displays as such) after each comma. If this is how your json_encode() call outputs it, you can run an additional measure to ensure those spaces aren't there before returning it to your AJAX function:
$encoded_text = str_replace(" ","",$encoded_text);
Don't use that if you may have data which has spaces normally. I'm not sure if that is what is causing it to not work, but that is the only difference in the strings you showed us.
You can also use JSONLint to validate your JSON for free.

Categories