我们如何使用java代码在屏幕上打印此JSON数据?

时间:2017-02-27 11:46:17

标签: android arrays json arraylist

我想用java Code打印数组列表..... 我必须显示每个学生出勤

ArrayList text_view_list = new ArrayList();

        JSONObject jsonObject=new JSONObject(response_view);


        for(int x=0;x<jsonObject.length();x++) {

            Iterator<String> iterator =  jsonObject.keys();
            while (iterator.hasNext()) {
                String key = iterator.next();

                student_name=key.replaceAll("[0-9-_]","  ");

                header[x]+=student_name;
                JSONArray jsonArray = jsonObject.getJSONArray(key);

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

                    text_view_list.add(new ViewAttendanceData(jsonObject2.getString("status"), jsonObject2.getString("date")));

                }
            }
        }       

标题是字符串数组,在替换号码后存储学生姓名 This Type of data Which i want to Show in Screen

代码哪个显示数据有点像这样:

TableLayout stk =(TableLayout)rootView.findViewById(R.id.id_table_V_Atte);

            for (int i=0;i<header.length;i++){
                TableRow tableRow=new TableRow(context);
                TextView tv0 = new TextView(context);
                for(int j=0;j<1;j++){
                    tv0.setText(""); //Dont Know What we have to write

                    tv0.setPadding(5, 10, 10, 10);

                    tv0.setGravity(Gravity.CENTER);
                    tv0.setTextColor(Color.GREEN);
                    tableRow.addView(tv0);
                    break;

                }stk.addView(tableRow);
            }




My JSON is 

            {
              "Mr. VICKRAM  SINGH_269": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIVEK KUMAR YADAV_276": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VICKY  KUMAR_270": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIBHUTI  VATS_266": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. Amit  Sharma_282": [
                {
                  "status": "A",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. SUNIL  CHOUDHARY_260": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIVEK  LAKHERA_277": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIBHU  VISHAL_263": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. SUNIDHI  KUMARI_255": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIBUDH  TIWARI_268": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIBHU  SINGH_262": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIVEK  MALIK_280": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIBHUTI  PANDYA_265": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIVEK  MAURYA_281": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. SUNIL  GOYAL_261": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. SUNIDHI  SINGH_256": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIVEK KUMAR VIDWAT_275": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VICKY HARESH GANGAR_271": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. SUNIL  CHOUDHARY_258": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIVEK KUMAR VERMA_274": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIVEK KUMAR VERMA_273": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIBHUTI  CHOUDHARY_264": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIBRANT  SAHU_267": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. SUNIDHI UMESH KUMAR_257": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIVEK  MALAV_278": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. SUNIL  CHOUDHARY_259": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. SUNEETH AKSHAR KORLEPARA_254": [
                {
                  "status": "A",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIVEK KUMAR UPADHYAY_272": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ],
              "Mr. VIVEK  MALIK_279": [
                {
                  "status": "P",
                  "date": "2017-02-05"
                },
                {
                  "status": "P",
                  "date": "2017-02-06"
                }
              ]`enter code here`
            }



Thanks In advance

2 个答案:

答案 0 :(得分:0)

JSONObject jsonArray1=new JSONObject(response_view); //Getting Response from server

                        for(int x=0;x<jsonArray1.length();x++) {
                            JSONArray jsonArray = jsonArray1.getJSONArray(x);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                jsonObject = jsonArray.getJSONObject(i);
                                text_view_list.add(new ViewAttendanceData(jsonObject.getString("status"), jsonObject.getString("date")));
                            }

                        }

试试这个希望它有所帮助,你的JSON响应不正确

答案 1 :(得分:0)

The keys of the JsonObject are dynamic. So we can parse the data as the help of below methods.

private void parseData(String result) {
    try {
        if (result != null) {
            JSONObject data = new JSONObject(result);
            if(data != null) {}
                Iterator<String> it = data.keys();
                while (it.hasNext()) {
                    String key = it.next();
                    try {
                        System.out.println("Data of: " + key);
                        if (data.get(key) instanceof JSONArray) {
                            JSONArray arry = data.getJSONArray(key);
                            int size = arry.length();
                            for (int i = 0; i < size; i++) {
                                parseJson(arry.getJSONObject(i));
                            }
                        } else if (data.get(key) instanceof JSONObject) {
                            parseJson(data.getJSONObject(key));
                        }
                    } catch (Throwable e) {
                        try {
                            System.out.println(key + ":" + data.getString(key));
                        } catch (Exception ee) {
                        }
                        e.printStackTrace();

                    }
                }
            }
        }
    } catch (Exception e){}
}

private void parseJson(JSONObject object) {
    try {
        if(object != null) {
            System.out.println("Status:" + data.getString("status") + ", date:" + data.getString("date"));
        }
    } catch (Exception e) {}
}