解析不带表名的JSON

时间:2018-08-22 13:23:52

标签: android json parsing hashmap

我正在尝试使用JSON解析数据库。 但是,我不知道如何解析JSON文件以获取表的名称和值,而无需声明表的名称已预先加载

 for(int i=0;i<jsonArray.length();i++){

           JSONObject item = jsonArray.getJSONObject(i);

           String id = item.getString(TAG_email);
           String name = item.getString(TAG_name);
           String address = item.getString(TAG_id);

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

           hashMap.put(TAG_EMAIL, email);
           hashMap.put(TAG_NAME, name);
           hashMap.put(TAG_ID, id);

           mArrayList.add(hashMap);
       }

这意味着,如果我必须获取电子邮件地址,我必须告诉代码以解析“ email”,但是可以单独加载所有表名(email,name,id)并解析值?

1 个答案:

答案 0 :(得分:0)

尝试

JsonArrayRequest getJsonData = new JsonArrayRequest("<your_url>,
        new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                for (int i = 0; i < response.length(); i++) {
                    try {
                        Users mList = new Users();

                        JSONObject obj = response.getJSONObject(i);

                        JSONArray answers = obj.getJSONArray("emails");

                        long count = answers.length();
                        if(count > 0){
                            //setter methods
                        }

                        mList.setIDL(obj.getString("_id"));
                        list.add(mList);

                    } catch (JSONException e) {
                        Log.d("JSONError", e.toString());
                    }

                }
                adapter.notifyDataSetChanged();
                //your listview findViewById().....
                listView.setVisibility(View.VISIBLE);

            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        VolleyLog.d(TAG, "Error: " + error.getMessage());
        Toast.makeText(getApplicationContext(),"Error in fetchinf data",Toast.LENGTH_LONG).show();

    }
});