如何解析listview android中的json对象和json数组

时间:2016-03-29 10:58:07

标签: android json

我是Android app的初学者。我遇到麻烦如何解析json对象和json数组到android中的listview。这是我的json输出

使用JSON更正更新

{status: "ok", listUsers: [{"id":2,"username":"myusername","name":"myname","email":"myemail","password":"mypassword","groupid":1,"type":"mytype"},{"id":3,"username":"myusername","name":"myname","email":"myemail2","password":"mypassword2","groupid":1,"type":"mytype"},{"id":4,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":5,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":6,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":7,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":8,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":9,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":10,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":11,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"1"},{"id":12,"username":"username1","name":"name1","email":"email1","password":"pass1","groupid":1,"type":"type1"},{"id":13,"username":"yuwah","name":"yu","email":"mail@gmail.com","password":"pass1","groupid":1,"type":"type1"},{"id":14,"username":"myusername","name":"myname","email":"myemail2","password":"mypassword2","groupid":1,"type":"mytype"}] }

任何人都可以解释我该怎么做。我正在搜索所有主题,但我仍然无法得到它。谢谢。 这是我的代码块

public class MainActivity extends ListActivity {

String url = "http://staging.workberryplus.com/mobile/listUsers/1";
ProgressDialog PD;

ArrayList<String> listUsers;
ArrayAdapter<String> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    listUsers = new ArrayList<String>();

    PD = new ProgressDialog(this);
    PD.setMessage("Loading.....");
    PD.setCancelable(false);

    adapter = new ArrayAdapter(this, R.layout.items, R.id.tv, listUsers);
    setListAdapter(adapter);

    MakeJsonArrayReq();
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
}

private void MakeJsonArrayReq() {
    PD.show();

    //JsonArrayRequest jr=new JsonArrayRequest(url, listener, errorListener)


    final StringRequest jreq = new StringRequest(url,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    for (int i = 0; i < response.length(); i++) {
                        try {
                            Log.d("Response","->"+response);
                            JSONObject jo = new JSONObject(response);
                            JSONArray jarray = jo.getJSONArray("listUsers");
                            JSONObject jo2 = jarray.getJSONObject(i);
                            String name = jo2.getString("name");
                            listUsers.add(name);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    PD.dismiss();
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });

    MyApplication.getInstance().addToReqQueue(jreq, "jreq");
}

}

3 个答案:

答案 0 :(得分:2)

     try {
           JSONObject jo = new JSONObject(response);
           JSONArray jarray =jo.getJSONArray("listUsers");  

           for (int i = 0; i < jarray.length(); i++){                        
                JSONObject jo2 = jarray.getJSONObject(i);
                String name = jo2.getString("name");
                listUsers.add(name);
              }
            } catch (JSONException e) {
                e.printStackTrace();
            }

答案 1 :(得分:0)

转换对Json对象

的响应的String
JSONObject jsonObj = new JSONObject(response);

然后执行以下操作

 try {
            if (jsonObj != null) {
                if (jsonObj.optString("status").equals("ok")) {
                    JSONArray jsonArray = jsonObj.optJSONArray("listUsers");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.optJSONObject(i);
                        if (jsonObject != null) {
                           //Do work here


                        }
                    }
}
}
}catch (Exception e1) {
                e1.printStackTrace();
            }

尝试此操作并相应地进行更改,并告诉我它是否适合您

答案 2 :(得分:0)

JSONObject jo2 = jarray.getJSONObject(i);

您正在迭代响应的长度(字符串)。你应该遍历jarray的长度