2

I used json_encode(); to convert string to json in php and then response it to android but I can't use the response, how can I convert the json to string? when I display the response it shows this :

"{\n'OK': \n[\n{\n'Name': 'MyName',\n'Gender':'Male'\n}\n]\n}" what shall I do?

thank you

3 Answers3

1

Since you're just converting a string to json, you're not returning a JSONObject or JSONArray, according to: http://php.net/manual/en/function.json-decode.php

If you must return a string, you may have to use some json library or write your own parser.

If that doesn't sound appealing, I recommending returning a JSONObject or JSONArray with one element.

For example:

php

echo json_encode( array('result' => 'the string you are encoding') );

java

JSONObject json = new JSONObject( encodedStringResponseFromPhp );
String theStringYouEncoded = (String) json.get( "result" );

You'll need to add a throws JSONException to the function you add this java code too or put it inside a try catch block.

Kacy
  • 3,330
  • 4
  • 29
  • 57
0

Have you tried using a JSON-Library like https://code.google.com/p/json-simple/? Looks like you need some help decoding the string.

Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108
0

Edit: You should use the json2.js library from Douglas Crockford. It provides some extra features and better/older browser support.

Read more...

Community
  • 1
  • 1
Iman Marashi
  • 5,593
  • 38
  • 51