繁体   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