Jump to content

JSON Array with a [


savagenoob

Recommended Posts

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));
?>

Link to comment
https://forums.phpfreaks.com/topic/252475-json-array-with-a/
Share on other sites

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;
}
}

 

Link to comment
https://forums.phpfreaks.com/topic/252475-json-array-with-a/#findComment-1294448
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/252475-json-array-with-a/#findComment-1294452
Share on other sites

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));
?>

Link to comment
https://forums.phpfreaks.com/topic/252475-json-array-with-a/#findComment-1294477
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.