本文共 6287 字,大约阅读时间需要 20 分钟。
1.json转换为JAVA
@Test public void jsonToJAVA() { System.out.println("json字符串转java代码"); String jsonStr = "{\"password\":\"\",\"username\":\"张三\"}";//添加转义字符 JSONObject jsonObj = JSONObject.fromObject(jsonStr); String username = jsonObj.getString("username"); String password = jsonObj.optString("password"); System.out.println("json--->java\n username=" + username + "\t password=" + password); }
运行结果:
json字符串转java代码json--->java username=张三 password=
2.java转换为JSON
@Test public void javaToJSON() { System.out.println("java代码封装为json字符串"); JSONObject jsonObj = new JSONObject(); jsonObj.put("username", "张三"); jsonObj.put("password", ""); System.out.println("java--->json \n" + jsonObj.toString()); }
运行结果:
java代码封装为json字符串java--->json { "username":"张三","password":""}
3.json转换为XML
@Test public void jsonToXML() { System.out.println("json字符串转xml字符串"); String jsonStr = "{\"password\":\"\",\"username\":\"张三\"}"; JSONObject json = JSONObject.fromObject(jsonStr); XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setRootName("user_info"); xmlSerializer.setTypeHintsEnabled(false); String xml = xmlSerializer.write(json); System.out.println("json--->xml \n" + xml); }
运行结果:
json字符串转xml字符串json--->xml张三
4.XML转换为JSON
@Test public void xmlToJSON() { System.out.println("xml字符串转json字符串"); String xml = ""; XMLSerializer xmlSerializer = new XMLSerializer(); JSON json = xmlSerializer.read(xml); System.out.println("xml--->json \n" + json.toString()); } 张三
运行结果:
xml字符串转json字符串五月 20, 2017 9:20:18 下午 net.sf.json.xml.XMLSerializer getType信息: Using default type stringxml--->json { "password":[],"username":"张三"}
5.javaBean转换为JSON
@Test public void javaBeanToJSON() { System.out.println("javabean转json字符串"); UserInfo userInfo = new UserInfo(); userInfo.setUsername("张三"); userInfo.setPassword(""); JSONObject json = JSONObject.fromObject(userInfo); System.out.println("javabean--->json \n" + json.toString()); }
运行结果:
javabean转json字符串javabean--->json { "password":"","username":"张三"}
6.javaBean转换为XML
@Test public void javaBeanToXML() { System.out.println("javabean转xml字符串"); UserInfo userInfo = new UserInfo(); userInfo.setUsername("张三"); userInfo.setPassword(""); JSONObject json = JSONObject.fromObject(userInfo); XMLSerializer xmlSerializer = new XMLSerializer(); String xml = xmlSerializer.write(json, "UTF-8"); System.out.println("javabean--->xml \n" + xml); }
运行结果:
javabean转xml字符串javabean--->xml张三
贴上全部代码:
package org.xiaowu.json.demo;import org.junit.Test;import net.sf.json.JSON;import net.sf.json.JSONObject;import net.sf.json.xml.XMLSerializer;public class JsonObjectDemo { @Test public void jsonToJAVA() { System.out.println("json字符串转java代码"); String jsonStr = "{\"password\":\"\",\"username\":\"张三\"}"; JSONObject jsonObj = JSONObject.fromObject(jsonStr); String username = jsonObj.getString("username"); String password = jsonObj.optString("password"); System.out.println("json--->java\n username=" + username + "\t password=" + password); } @Test public void javaToJSON() { System.out.println("java代码封装为json字符串"); JSONObject jsonObj = new JSONObject(); jsonObj.put("username", "张三"); jsonObj.put("password", ""); System.out.println("java--->json \n" + jsonObj.toString()); } @Test public void jsonToXML() { System.out.println("json字符串转xml字符串"); String jsonStr = "{\"password\":\"\",\"username\":\"张三\"}"; JSONObject json = JSONObject.fromObject(jsonStr); XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setRootName("user_info"); xmlSerializer.setTypeHintsEnabled(false); String xml = xmlSerializer.write(json); System.out.println("json--->xml \n" + xml); } @Test public void xmlToJSON() { System.out.println("xml字符串转json字符串"); String xml = ""; XMLSerializer xmlSerializer = new XMLSerializer(); JSON json = xmlSerializer.read(xml); System.out.println("xml--->json \n" + json.toString()); } @Test public void javaBeanToJSON() { System.out.println("javabean转json字符串"); UserInfo userInfo = new UserInfo(); userInfo.setUsername("张三"); userInfo.setPassword(""); JSONObject json = JSONObject.fromObject(userInfo); System.out.println("javabean--->json \n" + json.toString()); } @Test public void javaBeanToXML() { System.out.println("javabean转xml字符串"); UserInfo userInfo = new UserInfo(); userInfo.setUsername("张三"); userInfo.setPassword(""); JSONObject json = JSONObject.fromObject(userInfo); XMLSerializer xmlSerializer = new XMLSerializer(); String xml = xmlSerializer.write(json, "UTF-8"); System.out.println("javabean--->xml \n" + xml); }} 张三
package org.xiaowu.json.demo;public class UserInfo { public String username; public String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
以及项目所必须导入的jar包,注意版本信息
转载地址:http://tioux.baihongyu.com/