Jsoup+json-lib解析xml带中括号的数组Json数据

xiaoxiao2021-03-01  3

<?xml version="1.0" encoding="UTF-8"?> <result> <status>1</status> <!-- 带中括号的Json数据 --> <info> [{"Student":{"userName":"张三","age":"25","address":"中国大陆","Email":"zhangsan@sina.com"}},{"Student":{"userName":"李四","age":"26","address":"中国台湾","Email":"lisi@sina.com"}}] </info> </result>

 

package com.org.entity; import java.io.Serializable; /** *@Author:liangjilong *@Date:2013-10-30 *@Version:1.0 *@Email:liangjilong51job@qq.com *@Description: */ public class Student implements Serializable{ private String userName; private String age; private String address; private String Email; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return Email; } public void setEmail(String email) { Email = email; } }

 

package com.org.domain; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import com.org.entity.Student; /** * @Author:liangjilong * @Date:2013-10-30 * @Version:1.0 * @Email:liangjilong51job@qq.com * @Description:解析XML里面带中括号的JSON数组的数据 * http://bbs.csdn.net/topics/380187164 */ @SuppressWarnings("all") public class DoMain { public static void main(String[] args) throws Exception { testJson(); String fileName="src/stu.xml"; File file=new File(fileName); Document doc=Jsoup.parse(file,"utf-8"); if(doc!=null){ //使用Jsoup去解析xml的info节点的Json数据 String info=doc.select("info").text().toString(); int start = info.indexOf("["); String newJson = info.substring(start, info.lastIndexOf("]")+1);//组装成新的Json数据 //json-lib-2.2.3-jdk15.jar JSONArray jsonArr=JSONArray.fromObject(newJson); JSONObject objRoot=null; /** * 遍历Json数据 */ for(int i=0,l=jsonArr.size();i<l;i++){ objRoot=jsonArr.getJSONObject(i); Object obj=objRoot.get("Student");//Student节点 JSONObject jsonObj=JSONObject.fromObject(obj); /** * 方法一、根据key获取value值 */ System.out.println(jsonObj.get("userName")+"\t"+jsonObj.get("age")+"\t"+jsonObj.get("address")+jsonObj.get("Email")); System.out.println("=================================================================="); /** * 方法二、根据对象化获取值 */ Student stu=(Student)jsonObj.toBean(jsonObj,Student.class); System.out.println(stu.getUserName()+"\t"+stu.getAge()+"\t"+stu.getAddress()+"\t"+stu.getEmail()); } } } /** * 文件处理 * @param content * @param filePath * @return */ public static boolean write(String content, String filePath) { boolean flag = true; try { Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "utf-8")); out.write("\n" + content); out.close(); } catch (Exception ex) { ex.printStackTrace(); return false; } return flag; } /** * JsonObject,JsonPrimitive,JsonObject的使用 * */ private static void testJson() { JsonObject jsonObj = new JsonObject(); JsonArray jsonArr = new JsonArray(); JsonObject json_Obj = new JsonObject(); json_Obj.add("key1", new JsonPrimitive("value")); json_Obj.add("key2", new JsonPrimitive(1)); json_Obj.add("key3", new JsonPrimitive(false)); jsonArr.add(json_Obj); jsonObj.add("arr", jsonArr); //获取带有中括号数组的Json数据 System.out.println("str:\t"+jsonObj.toString()); System.out.println("key=\t"+jsonObj.get("arr")); } }

 

转载请注明原文地址: https://www.6miu.com/read-3850149.html

最新回复(0)