大家好,又见面了,我是你们的朋友全栈君。
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)); }
}
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/137613.html原文链接:https://javaforall.cn