关于 java JSONArray JSONObject转换问题

    xiaoxiao2026-05-03  11

    import lhy.client.json.JSONArray; import lhy.client.json.JSONException; import lhy.client.json.JSONObject; public class MyJson { public static void main(String[] args) throws JSONException { JSONArray ja=new JSONArray(); JSONObject jo=new JSONObject(); jo.put("1", "a"); jo.put("2", "b"); //ja.put(jo);此处不能写为jo.toString(), ja.put(jo.toString());//如果写成jo.toString()输出ja显示为["{\"1\":\"a\"}"] System.out.println(ja); for(int i=0;i<ja.length();i++){ //如果在加入JSONArray时,写为jo.toString(),会报错:JSONException: JSONArray[0] is not a JSONObject. System.out.println(ja.getJSONObject(i)); } }

    }

    相关资源:Java JSON与Object互转源代码
    最新回复(0)