java解析处理xml和json. 应该都有很多实现的方式. 这里总结其中一种方式.
使用 dom4j 生成和解析 xml 1 2 3 4 5 6 7 8 <dependency > <groupId > org.dom4j</groupId > <artifactId > dom4j</artifactId > <version > 2.1.1</version > </dependency >
生成xml 右侧截图为代码执行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @Test public void Test01 () { try { Document document = DocumentHelper.createDocument(); Element root = DocumentHelper.createElement("MCF" ); document.setRootElement(root); root.addAttribute("Name" , "处方" ); root.addAttribute("OprType" , "1" ); root.addElement("MCF.0" ).setText("" ); root.addElement("MCF.11" ).setText("11" ); Element drugList = root.addElement ("MCF.50.GRP" ); for (int i = 0 ; i < 3 ; i++) { Element d0 = drugList.addElement("MCF.50" ); d0.addElement("MCF.54" ).setText("54-" + i); d0.addElement("MCF.55" ).setText("55-" + i); } System.out.println(document.asXML()); }catch (Exception e){ e.printStackTrace(); } }
解析生成的xml
1 2 3 4 5 6 7 8 9 10 11 @Test public void Test02 () throws DocumentException { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <MCF Name=\"处方\" OprType=\"1\"> <MCF.0/> <MCF.11>11</MCF.11> <MCF.50.GRP> <MCF.50> <MCF.54>54-0</MCF.54> <MCF.55>55-0</MCF.55> </MCF.50> <MCF.50> <MCF.54>54-1</MCF.54> <MCF.55>55-1</MCF.55> </MCF.50> <MCF.50> <MCF.54>54-2</MCF.54> <MCF.55>55-2</MCF.55> </MCF.50> </MCF.50.GRP> </MCF>" ; Document doc = DocumentHelper.parseText(xml); System.out.println(doc.getRootElement().elementText("MCF.11" )); List<Element> list = doc.getRootElement().element("MCF.50.GRP" ).elements("MCF.50" ); for (Element e : list) { System.out.println(e.elementText("MCF.54" ) + " " + e.elementText("MCF.55" )); } }
使用jackson解析json 1 2 3 4 5 6 <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-databind</artifactId > <version > 2.13.1</version > </dependency >
生成json 生成一般不会直接生成. 会新建一个响应类创建一个响应对象. 然后给对象赋值. 再转成json
右侧截图为代码执行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @Test public void Test05 () throws Exception { ObjectMapper mapper = new ObjectMapper(); ObjectNode rootNode = mapper.createObjectNode(); ObjectNode childNode1 = mapper.createObjectNode(); ObjectNode childNode2 = mapper.createObjectNode(); childNode1.put("MCF.0" ,"" ); childNode1.put("MCF.11" ,"11" ); ArrayNode arrayNode = mapper.createArrayNode(); for (int i = 0 ; i < 3 ; i++) { ObjectNode childNode50 = mapper.createObjectNode(); childNode50.put("MCF.54" ,"54-" + i); childNode50.put("MCF.55" ,"55-" + i); arrayNode.add(childNode50); } childNode2.set("MCF.50" ,arrayNode); childNode1.set("MCF.50.GRP" ,childNode2); rootNode.set("MCF" ,childNode1); mapper.writeValueAsString(rootNode); String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode); System.out.println(jsonString); }
解析生成的json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Test public void Test06 () throws Exception { String json = "{ \"MCF\" : { \"MCF.0\" : \"\", \"MCF.11\" : \"11\", \"MCF.50.GRP\" : { \"MCF.50\" : [ { \"MCF.54\" : \"54-0\", \"MCF.55\" : \"55-0\" }, { \"MCF.54\" : \"54-1\", \"MCF.55\" : \"55-1\" }, { \"MCF.54\" : \"54-2\", \"MCF.55\" : \"55-2\" } ] } } }" ; JsonNode root = mapper.readTree(json); System.out.println(root.get("MCF" ).get("MCF.11" ).asText()); JsonNode prescriptions = root.get("MCF" ).get("MCF.50.GRP" ).get("MCF.50" ); for (int i = 0 ; i <= prescriptions.size() - 1 ; i++) { JsonNode drugs = prescriptions.get(i); System.out.println(drugs.get("MCF.54" ).asText() + " " + drugs.get("MCF.55" ).asText()); } }
对象转json 1 2 3 4 5 6 7 8 9 10 11 Student beanString = new Student(1 , 1 , 1 , "张三" ); List<Student> list = new ArrayList<>(); list.add(new Student(1 , 1 , 1 , "张三" )); list.add(new Student(1 , 1 , 2 , "李四" )); list.add(new Student(1 , 1 , 3 , "王五" )); System.out.println("JSONUtil.toJsonStr(beanString) = " + JSONUtil.toJsonStr(beanString)); System.out.println("JSONUtil.toJsonStr(list) = " + JSONUtil.toJsonStr(list));
对象转格式化的JSON 1 2 3 4 JSONUtil.toJsonPrettyStr(object); System.out.println("JSONUtil.toJsonPrettyStr(sortedMap) = " + JSONUtil.toJsonPrettyStr(sortedMap));
json解析 1 2 3 String html = "{\"name\":\"Something must have been changed since you leave\"}" ; JSONObject jsonObject = JSONUtil.parseObj(html); System.out.println("jsonObject.getStr(\"name\") = " + jsonObject.getStr("name" ));
XML字符串转为JSONObject 1 2 3 4 5 6 7 8 String s = "<sfzh>123</sfzh><sfz>456</sfz><name>aa</name><gender>1</gender>" ; JSONObject json = JSONUtil.parseFromXml(s); System.out.println("json.get(\"sfzh\") = " + json.get("sfzh" )); System.out.println("json.get(\"name\") = " + json.get("name" )); json.get("sfzh" ) = 123 json.get("name" ) = aa
JSON转换为XML 1 2 3 4 5 6 7 8 final JSONObject put = JSONUtil.createObj() .set("aaa" , "你好" ) .set("键2" , "test" ); final String s = JSONUtil.toXmlStr(put);System.out.println("s = " + s);
JSON转Bean 1 2 3 String json = "{\"ADT\":[[{\"BookingCode\":[\"N\",\"N\"]}]]}" ; Price price = JSONUtil.toBean(json, Price.class); System.out.println("price = " + price);