以某种格式插入数据和JSONObject

时间:2017-02-18 08:31:39

标签: java json

如何向JSONObject插入/添加数据 我正在尝试创建一个包含以下数据集的JSONObject。

   Collection<JSONObject> items = new ArrayList<JSONObject>();
   JSONArray array1 = new JSONArray();
   JSONObject item1 = new JSONObject();
   item1.put("scenario", array1);

我不知道如何继续。请帮助。

md5sum filename

2 个答案:

答案 0 :(得分:2)

试试这个:

JSONObject main = new JSONObject();
main.put("feature","testFeature");
JSONArray scenario = new JSONArray();
JSONObject s1 = new JSONObject();
s1.put("name","Add numbers");
s1.put("tags","@test");
JSONObject s2 = new JSONObject();
s2.put("name","Delete numbers");
s2.put("tags","@test123");
scenario.add(s1);
scenario.add(s2);
main.put("scenario", scenario);

答案 1 :(得分:1)

package demo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class Demo {

    public static void main(String[] args) {

        JSONObject json = new JSONObject();
        json.put("feature", "testFeature");
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonArray_json1 = new JSONObject();
        jsonArray_json1.put("name", "Add numbers");
        jsonArray_json1.put("tag", "@test");
        jsonArray.add(jsonArray_json1);
        JSONObject jsonArray_json2 = new JSONObject();
        jsonArray_json2.put("name", "Delete numbers");
        jsonArray_json2.put("tag", "@test123");
        jsonArray.add(jsonArray_json2);
        json.put("scenario", jsonArray);
        System.out.println(json);

    }
}