创建json数组java android [英] create json array java android

查看:97
本文介绍了创建json数组java android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我用此代码制作json数组的结果:

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

但是我想要的格式是:

[{
    "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.

任何想法都值得赞赏.

推荐答案

尝试下面的代码示例

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

这篇关于创建json数组java android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆