JSONObject与JSONArray的使用需要的jar包下载
https://mvnrepository.com/artifact/org.json/json/20180130
Files pom (5 KB) bundle (60 KB) View All 中点击bundle即可
1,JSONObject
json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value}
2,JSONArray
json数组,使用中括号[ ],只不过数组里面的项也是json键值对格式的
区别:
Json对象中添加的是键值对,JSONArray中添加的是Json对象
源码:
后台创建JSONObject方法一:直接put
response.setCharacterEncoding("utf-8");
JSONObject jsonObject =new JSONObject();
jsonObject.put("name", "爱你");
jsonObject.put("age", 12);
System.out.println(jsonObject);
System.out.println(jsonObject.isNull("name"));
System.out.println(jsonObject.isNull("xx"))