0

I am returning some SQL results from PHP to android.

I encode the JSON response as follows:

$json_string = json_encode($rows);
$json_string = str_replace("\\", "", $json_string, $i);
return $json_string;

Once i echo this back to android i get a string which when I place into a JSON validator gives valid JSON code.

HOWEVER....once i try to pass this valid string into a new JSONObject or JSONArray I receive an error that it cannot be converted.

I am sure that this is because of whitespace invisible characters.

I have encoded both my PHP and Java in utf-8 character set using the following in Java...

BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "utf-8"), 8);

and for PHP...

header('Content-Type: text/html; charset=utf-8');

but this has not solved the problem.

Any help greatly appreciated.

1 Answer 1

4

That's because you manipulate a valid json representaion with

$json_string = str_replace("\\", "", $json_string, $i);

Don't do that, json_encode already produces a valid json.

And the Content-Type should be application/json for only json output.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the fast reply but I have made the changes you suggest and the valid JSON string will still not convert to a JSONArray or Object
:) i mean the json string that you say is valid
im not sure what you mean. the string is valid as i have copy and pasted it into jsonlint to check it. for some reason though it will not accept it in JSONArray()
i mean f.ex. {"a":1,"b":2} is a valid json string. update your question to contain the samples you tried to feed JSONObject or JSONArray
@EHarpham : As pozs says, paste the JSON string that you are working with so we can see it. Also, post the code that you're using to parse the string and the logcat error showing where it's failing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.