0

i have a problem with jsonObject.

in android app i have:

try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
        Log.e("JSON Parser2", "json: "+json);
    }

the logcat is:

01-03 16:50:09.239: E/JSON Parser(17447): Error parsing data org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject 
01-03 16:50:09.239: E/JSON Parser2(17447): json: [][][]{"code":0,"message":"Segnalazione inviata correttamente"}

in php i have:

$response = array();
$response["code"] = $e['code'];
$response["message"] = $e['message'];
echo json_encode($response);

thanks

1
  • [][][] are spurious in the json you receive. Commented Jan 3, 2013 at 15:58

3 Answers 3

1

Change your PHP JsonObejct Creation Code as :

echo json_encode($response, JSON_FORCE_OBJECT);

and now you can parse current json string as :

  String str_final_json=json.replace("{}{}{}","");   

 // convert string to jsonObject
 JSONObject jObj = new JSONObject(str_final_json);

// get code from json object
String str_code=jObj.getString("code");

// get message from json object
String str_message=jObj.getString("message");
Sign up to request clarification or add additional context in comments.

8 Comments

i suppose you mean String str_code=jObj.getString("code");
but it don't work 01-03 17:16:29.039: W/System.err(20954): org.json.JSONException: No value for code (same for message)
@Atomico : you have added echo json_encode($response, JSON_FORCE_OBJECT); in php file ?
@Atomico : now what json string you are getting from server?plz share with me
just for your curiosity.. i found the problem :) in the code i wrong put echo json_encode($response, JSON_FORCE_OBJECT); more than 1 time.. my fault :)
|
0

Use JSONArray if your top level object is an array.

   try {
        jArr = new JSONArray(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
        Log.e("JSON Parser2", "json: "+json);
    }

Comments

0

Guess the issue is with your php code try to change your php code like below

$response = array("code" => $e['code'], "message" =>$e['message']);
echo json_encode($response);

2 Comments

01-03 17:32:29.279: E/JSON(21931): json: {}{}{}{"code":null,"message":null}
Try var_dump($e) and check if you get proper results.

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.