Search code examples
phpandroidjsonrestful-architecture

Extra curly braket in JSON & Parsing


I'm using rest ful service and it adds extra auto-numbered curly brackets as shown:

    {
  "budgets": [
    {
      "id": "1",
      "balance": "0",
      "addedBy": "Jhon",
      "NameBD": "Camping",
      "limitBD": "0",
      "DateBD": "2013-11-16 04:34:03"
    },
    {
      "id": "2",
      "balance": "0",
      "addedBy": "Aziz",
      "NameBD": "My Home",
      "limitBD": "0",
      "DateBD": "2013-11-16 10:33:11"
    },
    {
      "id": "3",
      "balance": "0",
      "addedBy": "Ryd",
      "NameBD": "My Department",


       "limitBD": "0",
          "DateBD": "2013-10-24 17:40:43"
        }
    ]
  }

and when i try to parse them it shows no value in "budgets" so i tried to view them in JSON Viewer:

enter image description here

here is my PHP function inside rest controller:

public function allbudget_get()
    {
        $data = $this->db->get('budget');
        $this->response( array('budgets'=>$data->result()) , 200);
    }

is there any way to remove them?

UPDATE: the app code is:

private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            // Locate the Class 
            bd = new ArrayList<AllBudgets>();
            // Create an array to populate the spinner 
            bdlist = new ArrayList<String>();
            // JSON file URL address
            jsonobject = JSONfunctions
                    .getJSONfromURL("http://localhost/SharedBudget/index.php/Budget/allbudget");

            try {
                // Locate the NodeList name
                jsonarray = jsonobject.getJSONArray("budgets");
                for (int i = 0; i < jsonarray.length(); i++) {
                    jsonobject = jsonarray.getJSONObject(i);

                    AllBudgets bdget = new AllBudgets();

                    bdget.setId(jsonobject.optString("id"));
                    bdget.setAddedBy(jsonobject.optString("addedBy"));
                    bdget.setNameBD(jsonobject.optString("NameBD"));
                    bdget.setDateBD(jsonobject.optString("DateBD"));
                    bdget.add(bdget);

                    // Populate spinner with budget names
                    bdlist.add(jsonobject.optString("NameBD"));

                }
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

Here is the JSON code for parsing

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url) {
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        // Download JSON data from URL
        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"), 8);
            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;
    }
}

UPDATE LOGCAT i also have setters & getters but i know they aren't the issue herethe logcat shows:

11-18 21:32:39.680: D/InputTransport(27646): Input channel constructed: name='ClientState{43f51ee0 uid 10152 pid 27646} (server)', fd=59
11-18 21:32:40.760: E/Error(27646): No value for budgets
11-18 21:32:40.760: W/System.err(27646): org.json.JSONException: No value for budgets
11-18 21:32:40.765: W/System.err(27646):    at org.json.JSONObject.get(JSONObject.java:354)
11-18 21:32:40.765: W/System.err(27646):    at org.json.JSONObject.getJSONArray(JSONObject.java:548)
11-18 21:32:40.765: W/System.err(27646):    at com.androidbegin.jsonspinnertutorial.MainActivity$DownloadJSON.doInBackground(MainActivity.java:49)
11-18 21:32:40.765: W/System.err(27646):    at com.androidbegin.jsonspinnertutorial.MainActivity$DownloadJSON.doInBackground(MainActivity.java:1)
11-18 21:32:40.765: W/System.err(27646):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-18 21:32:40.765: W/System.err(27646):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-18 21:32:40.765: W/System.err(27646):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-18 21:32:40.765: W/System.err(27646):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-18 21:32:40.765: W/System.err(27646):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-18 21:32:40.765: W/System.err(27646):    at java.lang.Thread.run(Thread.java:841)
11-18 21:32:41.230: D/dalvikvm(27646): GC_FOR_ALLOC freed 214K, 16% free 10193K/12064K, paused 20ms, total 21ms
11-18 21:32:41.230: I/dalvikvm-heap(27646): Grow heap (frag case) to 12.641MB for 1127536-byte allocation
11-18 21:32:41.255: D/dalvikvm(27646): GC_FOR_ALLOC freed <1K, 15% free 11294K/13168K, paused 22ms, total 22ms
11-18 21:32:41.265: D/AbsListView(27646): Get MotionRecognitionManager
11-18 21:32:41.275: D/InputTransport(27646): Input channel constructed: name='44913558 PopupWindow:42738418 (client)', fd=56
11-18 21:32:41.300: D/AbsListView(27646): unregisterIRListener() is called 
11-18 21:32:41.305: D/AbsListView(27646): unregisterIRListener() is called 
11-18 21:32:41.310: D/AbsListView(27646): unregisterIRListener() is called 
11-18 21:33:18.115: D/AbsListView(27646): unregisterIRListener() is called 

Solution

  • I found the answer, i used httpPost while the function uses GET so i just replaced it:

    HttpGet httpget = new HttpGet(url);
    

    Thanks to all of you who tried to help!