List> to org.json.JSONObject?

后端 未结 8 2022
借酒劲吻你
借酒劲吻你 2020-12-14 20:12
List> list = new ArrayList>();
Map map = new HashMap();

         


        
相关标签:
8条回答
  • 2020-12-14 20:55

    If you are using org.json.simple.JSONArray

    (https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1)

    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("abc", "123456");
    map.put("def", "hmm");
    list.add(map);
    
    JSONArray jsonArray = new JSONArray();
    jsonArray.addAll(listOfMaps);
    
    0 讨论(0)
  • 2020-12-14 20:57

    You have a map nested inside a list. you are trying to call the Map without ever iterating through the list first. JSON sometimes feels like magic but in fact it is not. I'll post some code in a moment.
    It would be more consistent with JSON to make a Map of Maps instead of a List of Maps.

    JSONObject json = new JSONObject(list);
    Iterator<?> it = json.keys();
    while (keyed.hasNext()) {
        String x = (String) it.next();
        JSONObject jo2 = new JSONObject(jo.optString(x));
    }
    
    0 讨论(0)
提交回复
热议问题