简体   繁体   中英

create json array java android

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

This is my out put from making json array from this code :

 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(); } 

But the format i want is :

[{
    "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"
}]

How to make this kind of format for this kind of making of array.

Any idea is appreciated.

Try this way

 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”子句。

To remove empty objects , before adding it to array check its length . if the length is equal to zero then no need to add current object to array. you can do this by using

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

Try below code sample

        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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM