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!

1
  • do some research on JSON. JSONArray will do what you need. Also the Json which you have shown above is not valid Commented May 1, 2013 at 11:03

3 Answers 3

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.

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

Comments

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

1 Comment

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

Comments

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.