【问题标题】:How we Print this JSON data on Screen using java code?我们如何使用 java 代码在屏幕上打印这个 JSON 数据?
【发布时间】:2017-02-27 11:46:16
【问题描述】:

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

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

【问题讨论】:

  • 显然你的响应不是json数组
  • 我的响应是存储在字符串变量名中作为 response_view

标签: android arrays json arraylist


【解决方案1】:
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) {}
}

【讨论】:

    【解决方案2】:
    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 响应不正确

    【讨论】:

    • 我的响应是存储在字符串变量名中作为 response_view
    • 我知道。所以你的响应首先有 JSONObject,然后你检索 JSONArray。发布错误日志
    • W/System.err:org.json.JSONException:不是原始数组:类 org.json.JSONObject W/System.err:在 org.json.JSONArray.(JSONArray. java:116) W/System.err: 在 com.visioneering.softedge.vems.asyncTasks.View_Attendance_API$override.onPostExecute(View_Attendance_API.java:162) W/System.err: 在 com.visioneering.softedge.vems.asyncTasks。 View_Attendance_API$override.access$dispatch(View_Attendance_API.java)
    • JSONArray jsonArray = new JSONArray(jsonArray1);将此行替换为 JSONArray jsonArray = jsonArray1.getJSONArray(x);
    • W/System.err: org.json.JSONException: No value for (0) W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 2016-09-19
    • 1970-01-01
    相关资源
    最近更新 更多