0

Generally, I post a requet to PHP server to query a mysql table's data set and then it will send back to Andorid to parsing the resonse string(a JSONArray string) without problem. The PHP code section used is as below:

....
if($result = mysql_query($sql, $link))
{
    if (mysql_num_rows($result) != 0) 
    {   
      while($row2 = mysql_fetch_array($result,MYSQL_ASSOC))
      {
          $data[]=$row;
      }
    }
}

print_r(urlencode(json_encode($data))); 
if (is_resource($result)) 
mysql_free_result($result);
mysql_close($link);

However, if I add one more pars of key-value string to $data array after completing sql query by using the method in php code below:

$data['new_key'] = "new_value";

There will have the JSONArry Exception prompt in android side while parsing the new JSONArray String: ..... (The value part is double bytes' Chinese Characters)

Value {"new_key":"new_value","0":{"old_key1":"old_value1" ,"old_key2":"old_value2","old_key3":"old_value3",.....}} of type org.json.JSONObject cannot be converted to JSONArray

If you don't mind, please help and point me out where I was wrong and how to solve it, thanks !

4
  • This is probably related to the fact that you're treating $data as a sequential array and then an associative array. Could you post the code that parses it on the android side? Commented May 18, 2012 at 11:47
  • at which line you are putting $data['new_key'] = "new_value"; Commented May 18, 2012 at 12:46
  • Thanks for jjm ! And the final $data array was encoded as JSONArray in PHP side before post to Android. Then Android parses the JSONArray String and the error prompted. Commented May 18, 2012 at 14:30
  • Thanks for Akki, the code $data['new_key'] = "new_value"; was added after the while loop completed. Commented May 18, 2012 at 14:33

1 Answer 1

1

Try this:

$data[]['new_key'] = "new_value";

Also I think you need to remove the print_r and replace it with echo:

echo urlencode(json_encode($data)); 

(You can also remove the urlencode by the way.)

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

3 Comments

Thanks woozy ! First I follows your suggestion change the code. Android parse the JSONArray is OK without error. However, Another JSONException prompted as: org.json.JSONException: No value for new_key. From the Log of reponse string shonw on the LogCat, the JSONArray string is look like this: {"old_key1":"old_value1" ,"old_key2":"old_value2","old_key3":"old_value3",.....},{"new_key":"new_value"}]. Look at the end of the string, the new key/value pair was located in the separated bracket that makes it can not be detected and parsing by JSONObject method. Thanks again for futher comment !
Humm... I thaught you wanted to create a JSNON array like that one. If you don't want you should use: $data[0]['new_key'] = "new_value" instead.
Thanks for woozy again ! follows your suggestion, I used "$data[0]['new_key'] = "new_value";" instead. And there is not Exception prompting... its WORLK !

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.