关于 java JSONArray JSONObject转换问题

来源:互联网 发布:农村淘宝下载安装2017 编辑:程序博客网 时间:2024/04/30 05:22
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));
}

}


}



0 0
原创粉丝点击