0

I was looking for a simple example on parsing a JSON file, using the loopj AsyncHttpClient. But so far I could not find any useful information. :(

Simple JSON file to parse:

{
    "contact": [
        {
                "id": "10",
                "name": "Tom",
                "email": "tom@gmail.com"

                }
        }
  ]
}

I would be grateful for any suggestion. Thanks!!

Edmond Tamas
  • 3,148
  • 9
  • 44
  • 89

3 Answers3

2

You need to get the json first. I assume you have done that

{  // json object node 
    "contact": [ //json array contact
        {     // json object node

To parse

JSONObject jb = new JSONObject("your json");
JSONArray con = jb.getJSONArray("contact");
JSONObject contact = (JSONObject) con.getJSONObject(0);
String id = contact.getString("id");
String name = contact.getString("name");
String id = contact.getString("id");
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0
public class MainActivity extends Activity {

    private ProgressDialog pdialog;

    private static String url = "http://highspecificationservers.com/apk/webservice.php";

    private static final String TAG_STUDENT = "student";
    private static final String TAG_FNAME = "fname";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_MOBILE = "mobile";

    JSONArray student = null;

    ArrayList<HashMap<String, String>> studentlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        studentlist = new ArrayList<HashMap<String, String>>();

        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                String fname = ((TextView) view.findViewById(R.id.fname))
                        .getText().toString();
                String cost = ((TextView) view.findViewById(R.id.email))
                        .getText().toString();
                String description = ((TextView) view.findViewById(R.id.mobile))
                        .getText().toString();

                Intent in = new Intent(getApplicationContext(),
                        SingleContactActivity.class);
                in.putExtra(TAG_FNAME, fname);
                in.putExtra(TAG_EMAIL, cost);
                in.putExtra(TAG_MOBILE, description);
                startActivity(in);

            }
        });

        new GetStudent().execute();

    }

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

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pdialog = new ProgressDialog(MainActivity.this);
            pdialog.setMessage("please wait");
            pdialog.setCancelable(false);
            pdialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub

            ServiceHandler sh = new ServiceHandler();

            String jString = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response:", "> " + jString);

            if (jString != null) {
                try {
                    JSONObject Jsonobj = new JSONObject(jString);

                    student = Jsonobj.getJSONArray(TAG_STUDENT);

                    for (int i = 0; i < student.length(); i++) {
                        JSONObject c = student.getJSONObject(i);
                        String fname = c.getString(TAG_FNAME);
                        String email = c.getString(TAG_EMAIL);
                        String mobile = c.getString(TAG_MOBILE);

                        HashMap<String, String> student = new HashMap<String, String>();

                        student.put(TAG_FNAME, fname);
                        student.put(TAG_EMAIL, email);
                        student.put(TAG_MOBILE, mobile);

                        studentlist.add(student);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (pdialog.isShowing())
                pdialog.dismiss();
            ListAdapter adapter = new SimpleAdapter(MainActivity.this,
                    studentlist, R.layout.list_item, new String[] { TAG_FNAME,
                            TAG_EMAIL, TAG_MOBILE }, new int[] { R.id.fname,
                            R.id.email, R.id.mobile });

            setListAdapter(adapter);

        }

        private void setListAdapter(ListAdapter adapter) {
            // TODO Auto-generated method stub

        }

    }

    private ListView getListView() {
        // TODO Auto-generated method stub
        return null;
    }

}
Marin Atanasov
  • 3,266
  • 3
  • 35
  • 39
  • Could you post the relevant code only, please? You have included a lot of unnecessary code if I have understood it correctly. I can't see any use of the `loopj AsyncHttpClient` as the asker requested. If you are suggesting another way of achieving this, please state that, along with explanations of your code. Your answer would be much more useful (for the asker and future readers) if it had relevant code only with comments and explanations. Please update your answer accordingly. – kkuilla Sep 17 '14 at 09:09
-1

Go with this...

    // JSON Node names
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_EMAIL = "email";


 if (jsonStr != null) {
 try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String id = c.getString(TAG_ID);
                        String name = c.getString(TAG_NAME);
                        String email = c.getString(TAG_EMAIL);

                       // tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put(TAG_ID, id);
                        contact.put(TAG_NAME, name);
                        contact.put(TAG_EMAIL, email);

                        // adding contact to contact list
                        contactList.add(contact);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
Akshay
  • 6,029
  • 7
  • 40
  • 59
  • -1 it is just copy of popular by not good online tutorial code ... posting someone else code as ours without pointing to oryginal is **called stealing** – Selvin Apr 10 '14 at 13:03
  • no it is not ... where is the answer to AsyncHttpClient's part of the question? – Selvin Apr 10 '14 at 13:13