Gson parsing path element throw an exception - php

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"}

Related

Angular http.get error to retrieve backslash stored in mysql

I'm so frustrated with this.
I'm using Angular from CDN and I'm calling a $http.get request to a PHP file that prints all the result set (mysql) in JSON format, which is "well done".
But, I have stored some backslashes into the database as part of the value of a field. E.G:
{"Field 1":"user1"},{"Field 2":"some_data\2"}
That crashes the entire page and the data in the view is not being displayed. It's printed in the raw JSON file this way:
{"Field 2":"some_data�2"}
Currently I'm printing the resultant json "manually":
$outp = '{"Field 2":"'.$resultset['field_data'].'"}';
echo $outp;
I've tried everything I know: addslashes(), stripslashes(), htmlspecialchars(), json_encode()... Error is:
SyntaxError: Unexpected token in JSON at position 138742
Can anyone help me, please?

Smarty modifier to decode a json string

i created a simple modifier to decode a string that is encoded in json.
function smarty_modifier_json_decode($string)
{
return json_decode($string, true);
}
Now i have somewhere into my database a string like this:
[{"foo": "bar bar", "cool": "attr"},{"foo": "bar", "cool": "attr"},{"foo": "bar", "cool": "attr"}]
The problem is the following if i store the above string locally inside the php file in a string variable and after i run the json_decode i get back the array i should get back and works fine.
But for some reason that i dont know and idont udnertand the string that is stored into the database is probably not correctly saved in the correct character encoding, because i always get back NULL when i try to execute the fucntion on smarty:
{$data.custom6|json_decode|var_dump}
So my question is if i have this string, but i dont know how my software save it into the database what should i do to convert it into the correct encodinh and after process it with json_decode ?
Thanks
NOTE 1: i dont know how the backend is saving data into the database and i cnnot influence this step. i can only using a web interface insert my JSON string in the custom field and press a SAVE button –
NOTE 2: sorry for the mistake copying the file here, i placed $json instead $string. But it doesnt work.

php json_decode error quoted object property name expected

I try to create a script that decoding a simple JSON string in PHP and I get the following error:
quoted object property name expected
The string I try to decode is the following :
{"values":[{"url":"http://www.google.com","matches":"http|www|google|com"},{"url":"http://www.yahoo.com","matches":"http|www|yahoo|com"}]}
and the code I use to decoded is the following:
json_decode( $json_string );
I also have try to validate my json string in some online json validators, and the string seems to be fine.
Can someone please help me ?
Do you think the problem exists because of the double quotes ?
Update #1
Definetelly was a debuging issue. I place my experience here just to help other devs may come accross the same issue in the feature:
The problem was that my variable that came with the json string was html encoded so instead of the following string :
{"values":[{"url":"http://www.google.com","matches":"http|www|google|com"},{"url":"http://www.yahoo.com","matches":"http|www|yahoo|com"}]}
my variable came with the following string inside :
{"values":[{"url":"http://www.google.com","matches":"http|www|google|com"},{"url":"http://www.yahoo.com","matches":"http|www|yahoo|com"}]}
The mistake by my side was that I used the print_r method instead of the var_dump . This had as a result to print out the &quot as " in my page .
The json string is valid, and it works. You can add true for the second parameter of json_decode, and you get back an array.
Try the following:
$json_string = '{"values":[{"url":"http://www.google.com","matches":"http|www|google|com"},{"url":"http://www.yahoo.com","matches":"http|www|yahoo|com"}]}';
var_dump(json_decode($json_string, true));
It works for me.

how to remove hidden junk character after php json encode

I was created JSON array from json_encode using PHP. I gave my array to json_encode . It's created JSON array very well. And i feed this JSON array to my android apps. When i am gonna read this url at android it's return following EXCEPTION ERROR.
Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject
But when i am gonna create jason object at android adding this following line
jObj=new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
It's working perfectly.
But i don't want to need the my second type solution. I need the php solution when i am put my json_encode on php.
And also at IOS the JSON return NULL values. How can i fix in both IOS and Android
Thanks advance
The java.text.Normalizer is intended to do exactly this: remove unwanted unicode characters.
The normalize method will allow you to pass your CharSequence and return the "normalized", ASCII-only String.

Fetch data from xml in php

in my xml file there is field like this:
<state-code>GA</state-code>
now i am fetching data from it in php.
simplexml_load_file is returning the whole content as object.
no problem still now ,but when i am writing like this
$single_property->location->state-code
it is not fetching value.
but if it is or then no problem.but i don't have control over the xml file structure.have to find out another way.
what to do?please suggest.
Assuming all other code not shown is correct, you access a property with illegal chars (the -) like:
$single_property->location->{'state-code'}

Categories