Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 添加列表<;字符串>;去JSONArray_Java_Json - Fatal编程技术网

Java 添加列表<;字符串>;去JSONArray

Java 添加列表<;字符串>;去JSONArray,java,json,Java,Json,我正试图用JSON库来改进JSON。在momant,我正在创建要添加的JSONArray,以将列表中的所有值添加到其中,但我面临着这个问题 JSONArray类型中的方法put(int,boolean)不适用于参数(字符串,列表) 在这一行arrivalMoFr.put(“mon-fri”,timeEntries)如何将列表添加到JSONArray 谢谢你的帮助 代码: List<String> timeEntries = entry.getValue(); try

我正试图用JSON库来改进JSON。在momant,我正在创建要添加的JSONArray,以将列表中的所有值添加到其中,但我面临着这个问题

JSONArray类型中的方法put(int,boolean)不适用于参数(字符串,列表)

在这一行
arrivalMoFr.put(“mon-fri”,timeEntries)如何将列表添加到
JSONArray

谢谢你的帮助

代码:

    List<String> timeEntries = entry.getValue();

    try {
        JSONObject timeTable = new JSONObject();
        timeTable.put("route", route);

        JSONObject info = new JSONObject();
        info.put("direction", direction);

        JSONObject stops = new JSONObject();     
        stops.put("stops_name", key);

        JSONObject arrivals = new JSONObject();
        JSONArray arrivalMoFr  = new JSONArray();
                        //The error is here.
        arrivalMoFr.put("mon-fri", timeEntries);

        arrivals.put("mon-fri", arrivalMoFr);

        stops.put("arrival_time", arrivals);


        info.put("stops", stops);
        timeTable.put("info", info);

        System.out.println(timeTable.toString(3));


    }
{
   "route": "4",
   "info": {
      "stops": {
         "arrival_time": {"mon-fri": [[
            "05:04",
            "18:41",
            "19:11",
            "19:41",
            "20:11"
         ]]},
         "stops_name": "Heiweg "
      },
      "direction": "Groß Grönau"
   }
}
结果:

    List<String> timeEntries = entry.getValue();

    try {
        JSONObject timeTable = new JSONObject();
        timeTable.put("route", route);

        JSONObject info = new JSONObject();
        info.put("direction", direction);

        JSONObject stops = new JSONObject();     
        stops.put("stops_name", key);

        JSONObject arrivals = new JSONObject();
        JSONArray arrivalMoFr  = new JSONArray();
                        //The error is here.
        arrivalMoFr.put("mon-fri", timeEntries);

        arrivals.put("mon-fri", arrivalMoFr);

        stops.put("arrival_time", arrivals);


        info.put("stops", stops);
        timeTable.put("info", info);

        System.out.println(timeTable.toString(3));


    }
{
   "route": "4",
   "info": {
      "stops": {
         "arrival_time": {"mon-fri": [[
            "05:04",
            "18:41",
            "19:11",
            "19:41",
            "20:11"
         ]]},
         "stops_name": "Heiweg "
      },
      "direction": "Groß Grönau"
   }
}
您可以使用将
List
转换为JSON

List<String> listStrings = new ArrayList<String>();
listStrings.add("a");
listStrings.add("b");

Gson objGson = new Gson();
System.out.println(objGson.toJson(listStrings));

您不能向JSON添加
List
。您可以从列表中创建JSONArray,但您所要求的是不可能的。您还尝试使用错误参数的方法。当您尝试向该方法传递字符串和列表时,JSONArray的方法“put”需要一个int和一个boolean。请参阅我的编辑代码问题:为什么我从JSONArray的emd开始就得到了`[[`两次?当JSOn不是多维时,我更喜欢Gson。我的JSOn代码的Gson等效代码是什么?请参阅我的编辑代码问题:为什么我一开始就得到JSONArray的emd`[[`两次?
JSONArray att = new JSONArray(YourList);