0

我正在寻找一个使用 loopj AsyncHttpClient 解析 JSON 文件的简单示例。但到目前为止,我找不到任何有用的信息。:(

要解析的简单 JSON 文件:

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

                }
        }
  ]
}

我将不胜感激任何建议。谢谢!!

4

3 回答 3

2

您需要先获取json。我假设你已经这样做了

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

解析

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");
于 2014-04-10T12:46:40.683 回答
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;
    }

}
于 2014-09-17T08:42:09.233 回答
-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");
            }
于 2014-04-10T13:01:21.453 回答