1.什么是json
JSON:(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于 ECMAScript(欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。目前,Json处于数据交换语言的王者地位.
1.1 json的特点
(1)json是一种轻量级的数据交换格式
(2)json采用完全独立于计算机语言和操作系统的平台,不同的编程语言和操作系统json数据是一致的
(3)json易于阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输效率)
1.2 xml和json的区别
(1)xml:
1.json与js对象的转换
<script type="text/javascript"> // js对象 var obj = { name: "亚瑟", age: 23, address: "上海" }; document.write(obj.name + "<br/>"); var obj2 = { users: [{ name: "鲁班", age: 21, address: "北京" }, { name: "后裔", age: 26, address: "广州" }] }; document.write(obj2.users[0].address + "<br/>"); // json转换为js对象(方式一) var test1 = '{"name":"后裔","age":25}'; var obj3 = eval("(" + test1 + ")"); //必须带圆括号 document.write("name=" + obj3.name + ",age=" + obj3.age + "<br/>"); // json转换为js对象(方式二) var obj4 = JSON.parse('{ "name":"李白", "age":18, "address":"北京" }'); document.write(obj4.name + "<br/>"); </script>
注意:
(1)有些浏览器不支持JSON.parse这个方法,可以到官网下载json2.js添加到你的hlml中就可以了。
(2)为什么要 eval这里要添加 eval(“(” + test1 + “)”)//”呢?
原因在于:eval本身的问题。 由于json是以”{}”的方式来开始以及结束的,在JS中,它会被当成一个语句块来处理,所以必须强制性
的将它转换成一种表达式。
加上圆括号的目的是迫使eval函数在处理JavaScript代码的时候强制将 括号内的表达式(expression)转化为对象,而不是作为语(statement)来执行。举一个例子,例如对象字面量{},如若不加外层的括号,那么eval会将大括号识别为JavaScript代码块的开始
和结束标记,那么{}将会被认为是执行了一句空语句。所以下面两个执行结果是不同的:
alert(eval(“{}”); // return undefined
alert(eval(“({})”);// return object[Object]
2.Json与Java对象的转换
(1)使用 alibaba 的 fastJson
<!--json--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency>
(2)实例
public class MyJson { public static String jsonmap = "{\"001\":{\"name\":\"xiaohong\",\"password\":\"654321\"},\"002\":[{\"$ref\":\"$.001\"},{\"name\":\"xixi\",\"password\":\"789\"}]}"; public static String jsonuser = "{\"name\":\"xiaohong\",\"password\":\"654321\"}"; public static String jsonlist = "[{\"name\":\"xiaohong\",\"password\":\"654321\"},{\"name\":\"xixi\",\"password\":\"789\"}]"; public static void main(String[] args) { User user = new User(); user.setName("xiaohong"); user.setPassword("654321"); List<User> list = new ArrayList<User>(); list.add(user); list.add(new User("xixi","789")); Map map = new HashMap(); map.put("001",user); map.put("002",list); // 1. json字符串 转换为 java 对象 // toJsonString - java对象转换为json字符串 System.out.println(JSONObject.toJSONString(user)); System.out.println(JSONObject.toJSONString(list)); System.out.println(JSONObject.toJSONString(map)); // toJavaObject - json字符串转换为java对象 System.out.println(JSONObject.parseObject(jsonuser,User.class)); List list1 = JSONObject.parseObject(jsonlist, List.class); for(int i=0;i<list1.size();i++) { System.out.println(JSONObject.parseObject(JSONObject.toJSONString(list1.get(i)),User.class)); } Map map1 = JSONObject.parseObject(jsonmap, Map.class); Iterator iterator = map1.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry= (Map.Entry) iterator.next(); System.out.println("key :"+entry.getKey()+" value: " + entry.getValue()); } // Json -- 实体类转换为json字符串 System.out.println(JSON.toJSONString(user)); System.out.println(JSON.toJSONString(list)); System.out.println(JSON.toJSONString(map)); // json -- json 转 java对象 System.out.println(JSON.parseObject(jsonuser,User.class)); List list2 = JSON.parseObject(jsonlist, List.class); for(int i=0;i<list2.size();i++) { System.out.println(JSON.parseObject(JSON.toJSONString(list1.get(i)),User.class)); } Map map2 = JSON.parseObject(jsonmap, Map.class); Iterator iterator2 = map1.entrySet().iterator(); while (iterator2.hasNext()) { Map.Entry entry= (Map.Entry) iterator2.next(); System.out.println("key :"+entry.getKey()+" value: " + entry.getValue()); } } }
3.使用Java修改json字符串(JSON解析库使用Jackson)
(1)替换json字符串,指定的值
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class Main { public static void main(String[] args) { String jsonStr = "{\"code\":0,\"msg\":\"\",\"count\":3042,\"data\":[{\"UPN\":\"WR1.04.046.0166300023032100120230316104\",\"PN\":\"1.04.046.0166\",\"Lot\":null,\"DC\":\"20230316\",\"Qty\":3000,\"PositionCode\":\"1-1-27-1\",\"CreateTime\":\"2023-10-23 08:49:00\"},{\"UPN\":\"WR1.04.046.0166300023032100120230316512\",\"PN\":\"1.04.046.0166\",\"Lot\":null,\"DC\":\"20230316\",\"Qty\":3000,\"PositionCode\":\"1-1-27-2\",\"CreateTime\":\"2023-10-23 08:49:12\"},{\"UPN\":\"WR1.04.046.0166300023032100120230316434\",\"PN\":\"1.04.046.0166\",\"Lot\":null,\"DC\":\"20230316\",\"Qty\":3000,\"PositionCode\":\"1-1-27-3\",\"CreateTime\":\"2023-10-23 08:49:24\"},{\"UPN\":\"WR1.04.046.0166300023032100120230316999\",\"PN\":\"1.04.046.0166\",\"Lot\":null,\"DC\":\"20230316\",\"Qty\":3000,\"PositionCode\":\"1-1-27-5\",\"CreateTime\":\"2023-10-23 09:05:02\"},{\"UPN\":\"WR1.04.042.0074273022120800120221205927\",\"PN\":\"1.04.042.0074\",\"Lot\":null,\"DC\":\"20221205\",\"Qty\":2730,\"PositionCode\":\"1-1-1-3\",\"CreateTime\":\"2023-10-25 12:51:21\"}]}"; JSONObject jsonObject = JSONObject.parseObject(jsonStr); jsonObject.put("msg", "替换后的值"); System.out.println(jsonObject.toJSONString()); } }
(2)替换json字符串的key的值
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class Main { public static void main(String[] args) { String jsonStr = "{\"code\":0,\"msg\":\"\",\"count\":3042,\"data\":[{\"UPN\":\"WR1.04.046.0166300023032100120230316104\",\"PN\":\"1.04.046.0166\",\"Lot\":null,\"DC\":\"20230316\",\"Qty\":3000,\"PositionCode\":\"1-1-27-1\",\"CreateTime\":\"2023-10-23 08:49:00\"},{\"UPN\":\"WR1.04.046.0166300023032100120230316512\",\"PN\":\"1.04.046.0166\",\"Lot\":null,\"DC\":\"20230316\",\"Qty\":3000,\"PositionCode\":\"1-1-27-2\",\"CreateTime\":\"2023-10-23 08:49:12\"},{\"UPN\":\"WR1.04.046.0166300023032100120230316434\",\"PN\":\"1.04.046.0166\",\"Lot\":null,\"DC\":\"20230316\",\"Qty\":3000,\"PositionCode\":\"1-1-27-3\",\"CreateTime\":\"2023-10-23 08:49:24\"},{\"UPN\":\"WR1.04.046.0166300023032100120230316999\",\"PN\":\"1.04.046.0166\",\"Lot\":null,\"DC\":\"20230316\",\"Qty\":3000,\"PositionCode\":\"1-1-27-5\",\"CreateTime\":\"2023-10-23 09:05:02\"},{\"UPN\":\"WR1.04.042.0074273022120800120221205927\",\"PN\":\"1.04.042.0074\",\"Lot\":null,\"DC\":\"20221205\",\"Qty\":2730,\"PositionCode\":\"1-1-1-3\",\"CreateTime\":\"2023-10-25 12:51:21\"}]}"; JSONObject jsonObject = JSONObject.parseObject(jsonStr); int count = jsonObject.getIntValue("count"); jsonObject.put("total", count); jsonObject.remove("count"); System.out.println(jsonObject.toJSONString()); } }