1

I've got a json object, which is being collected into a function as string.
It contains array

{"officer_name":"V. M. ARORA"}{"officer_name":"Dr. C. P. REDDY"}{"officer_name":"ARTI CHOWDHARY"}{"officer_name":"JAGDISH SINGH"}

and here is the android code

public void func4(View view)throws Exception
{
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams rp = new RequestParams();
    rp.put("pLat", "SELECT officer_name FROM iwmp_officer");

    client.post("http://10.0.2.2/conc5.php", rp, new AsyncHttpResponseHandler() {
        public final void onSuccess(String response) {
            // handle your response here
            ArrayList<String> User_List = new ArrayList<String>();
            try { 
                 /* here I need output in an array,
                 and only names not the "officer_name" */
            } catch (Exception e) {
                tx.setText((CharSequence) e);
            }

            //tx.setText(User_List.get(1));
        }

        @Override
        public void onFailure(Throwable e, String response) {
            // something went wrong
            tx.setText(response);
        }               
    });
}

The output I've shown above is in String, need to get it in array. Please help!

Yogesh Kumar
  • 682
  • 1
  • 10
  • 29
  • do some research on JSON. `JSONArray` will do what you need. Also the Json which you have shown above is not valid – Naveen May 01 '13 at 11:03

3 Answers3

1

If the output you got is something like this.

String outputJson=[{"officer_name":"V. M. ARORA"}{"officer_name":"Dr. C. P. REDDY"}{"officer_name":"ARTI CHOWDHARY"}{"officer_name":"JAGDISH SINGH"}]

Then its a JSON Array. You can parse it as

JsonArray array=new JsonArray(outputJson);

Then loop this json array using

for(JsonObject jsonObj in array){

String officerName=[jsonObj getString("officer_name");

}

You can use something like above The mentioned code is not correct syntactically but yes conceptually correct. You can go ahead with this.

Avtar Guleria
  • 2,126
  • 3
  • 21
  • 33
1
List < String > ls = new ArrayList< String >();
JSONArray array    = new JSONArray( response );

for (int  i = 0; i < array.length() ; i++ ) {
    JSONObject obj = array.getJSONObject(Integer.toString(i));

    ls.add(obj.getString("officer_name"));
}

This would work

Touki
  • 7,465
  • 3
  • 41
  • 63
  • plz check my other ques, http://stackoverflow.com/questions/16353561/java-net-socketexceptionoperation-time-out-when-running-app-on-real-device – Yogesh Kumar May 03 '13 at 09:26
0
try {
                    JSONArray array= new JSONArray(response);
                    //array = new JSONArray(response);
                      for (int i = 0; i < array.length(); i++) { 
                          //JSONObject obj = response.getJSONArray(i);
                          JSONObject jsonLineItem = (JSONObject) array.getJSONObject(i);
                          String name_fd = jsonLineItem.getString("officer_name");
                          User_List.add(jsonLineItem.getString("officer_name"));
                          Log.d("JSONArray", name_fd+"   " +name_fd);

                      }


                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    tx.setText( e.toString());
                }
Yogesh Kumar
  • 682
  • 1
  • 10
  • 29