savagenoob Posted December 4, 2011 Share Posted December 4, 2011 I am writing a simple connector for an android application and my json_encode is outputting enclosing the array with []. This is throwing an error during the decode process because the array needs to start with a {. I know this has to do with the $output[] in the loop, but I dont know how to pass the values into the array any other way. I even tried to trim after doing the encode. Here is my code: <?php require_once ('includes/config.php'); require_once ('includes/connect.php'); $agency= mysql_query("SELECT agencyname FROM agency WHERE status ='Prospect' ORDER BY agencyname DESC")or die(mysql_error()); while($ageinfo = mysql_fetch_assoc($agency)) { $output[] = $ageinfo; } print(json_encode($output)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252475-json-array-with-a/ Share on other sites More sharing options...
KevinM1 Posted December 4, 2011 Share Posted December 4, 2011 JSON arrays also start with [. Curly braces (e.g., {}) are used to denote JSON objects. See: http://www.json.org/ How are you trying to decode this? Quote Link to comment https://forums.phpfreaks.com/topic/252475-json-array-with-a/#findComment-1294444 Share on other sites More sharing options...
savagenoob Posted December 4, 2011 Author Share Posted December 4, 2011 Using Java... import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; public class JSONfunctions { public static JSONObject getJSONfromURL(String url){ InputStream is = null; String result = ""; JSONObject jArray = null; //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); } //convert response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),; StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } try{ jArray = new JSONObject(result); }catch(JSONException e){ Log.e("log_tag", "Error parsing data "+e.toString()); } return jArray; } } Quote Link to comment https://forums.phpfreaks.com/topic/252475-json-array-with-a/#findComment-1294448 Share on other sites More sharing options...
KevinM1 Posted December 4, 2011 Share Posted December 4, 2011 I'm afraid I'm not all that familiar with Java. Have you tried asking on an Android forum, or on Stack Overflow? Because there's nothing in your PHP that jumps out at me. Your code produces an array that looks like: [agencyname1, agencyname2, agencyname3, ..., agencynamen] Which is a legit JSON array. So the problem has to be in your decode function, somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/252475-json-array-with-a/#findComment-1294452 Share on other sites More sharing options...
savagenoob Posted December 5, 2011 Author Share Posted December 5, 2011 Yeah, I figured. I was wondering if there was a way to remove the brackets before the print. Quote Link to comment https://forums.phpfreaks.com/topic/252475-json-array-with-a/#findComment-1294455 Share on other sites More sharing options...
savagenoob Posted December 5, 2011 Author Share Posted December 5, 2011 I read the json_encode manual (duh) and saw there is a JSON_FORCE_OBJECT option that removes the square brackets, but when I run it against my array it doesnt display at all... any help? <?php require_once ('includes/config.php'); require_once ('includes/connect.php'); $agency= mysql_query("SELECT agencyname, busphone FROM agency WHERE status ='Prospect' ORDER BY agencyname DESC")or die(mysql_error()); while($ageinfo = mysql_fetch_assoc($agency)) { $output[] = $ageinfo; } print(json_encode($output, JSON_FORCE_OBJECT)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252475-json-array-with-a/#findComment-1294477 Share on other sites More sharing options...
PFMaBiSmAd Posted December 5, 2011 Share Posted December 5, 2011 The 2nd parameter was only added in php5.3. What's your php version and do you have error_reporting set to E_ALL and display_errors and/or log_errors set to ON so that php would report and display/log all the errors it detects? Quote Link to comment https://forums.phpfreaks.com/topic/252475-json-array-with-a/#findComment-1294479 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.