簡體   English   中英

創建json數組java android

[英]create json array java android

[{
    "surveyid": 1
}, {}, {}, {}, {}, {}, {}, {
    "question": "1"
}, {
    "answer": "john"
}, {
    "question": "2"
}, {
    "answer": "Male"
}, {}, {
    "question": "3"
}, {}, {
    "answer": "Fishes"
}, {
    "answer": "Cats"
}, {
    "answer": "Dogs"
}]

這是我從此代碼制作json數組的結果:

 JSONArray myArray = new JSONArray(); try { for (View touchable: allTouchables) { JSONObject j = new JSONObject(); String className = touchable.getClass().getName(); if (className.equalsIgnoreCase("android.widget.TextView")) { TextView textview = (TextView) touchable; String tag = (String) textview.getTag(); System.out.println("tag " + tag); if (tag.matches("questions")) { String questionid = textview.getText().toString().split("[\\\\(\\\\)]")[1]; System.out.println("TextView touchable " + questionid); j.put("question", questionid); } else if (tag.matches("surveytitle")) { int questionid = textview.getId(); System.out.println("TextView touchable " + questionid); j.put("surveyid", questionid); } } else if (className.equalsIgnoreCase("android.widget.RadioButton")) { RadioButton value = (RadioButton) touchable; if (value.isChecked()) { System.out.println("RadioButton " + value.getText().toString()); j.put("answer".toString(), value.getText().toString()); } } else if (className.equalsIgnoreCase("android.widget.EditText")) { EditText value = (EditText) touchable; System.out.println("EditText " + value.getText().toString()); j.put("answer".toString(), value.getText().toString()); } else if (className.equalsIgnoreCase("android.widget.CheckBox")) { CheckBox value = (CheckBox) touchable; if (value.isChecked()) { System.out.println("CheckBoxButton " + value.getText().toString()); j.put("answer", value.getText().toString()); } } myArray.put(j); System.out.println("myArray j " + j); } System.out.println("myArray j " + myArray); } catch (JSONException e) { e.printStackTrace(); } 

但是我想要的格式是:

[{
    "surveyid": 1,
    "question": "1",
    "answer": "john"
}, {
    "surveyid": 1,
    "question": "2",
    "answer": "Male"
}, {
    "surveyid": 1,
    "question": "3",
    "answer": "Fishes"
}, {
    "surveyid": 1,
    "question": "3",
    "answer": "Cats"
}, {
    "surveyid": 1,
    "question": "3",
    "answer": "Dogs"
}]

如何為這種數組制作制作這種格式。

任何想法表示贊賞。

試試這個

 JSONObject question1 = new JSONObject(); try { question1.put("surveyid", "1"); question1.put("question", "1"); question1.put("answer", "john"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONObject question2 = new JSONObject(); try { question2.put("surveyid", "5"); question2.put("question", "6"); question2.put("answer", "john2"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONArray jsonArray = new JSONArray(); jsonArray.put(question1); jsonArray.put(question2); 

輸出中有許多空對象,這意味着您已向JSONArray中添加了一個空JSONObject,我認為循環塊代碼中存在一些不良邏輯,例如,如果沒有條件滿足“ if”和“ else if”,它將產生空對象,它需要一個“ else”子句。

要刪除空對象 ,請在將其添加到數組之前檢查其長度 如果長度等於零,則無需將當前對象添加到數組。 你可以通過使用

if(j.length()>0){
 myArray.put(j);
}

嘗試以下代碼示例

        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < 4; i++) {
            JSONObject object = new JSONObject();
            object.put("surveyid", surveyid);
            object.put("question", question);
            object.put("answer", answer);
            jsonArray.put(object);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM