Java解析Json数据,Java类型与Json数据类型的转换:
所用jar文件:json.org.jar
JsonHelper.java
package com.json;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
/**
* 1.将JavaBean转换为Map、JSONObject
* 2.将Map转换为JavaBean
* 3.将JSONObject转换为Map、JavaBean
* 用到的包有json.org.jar
* @author xiejunbo
* @email 624664181@qq.com
* @date Nov 17, 2014 12:47:45 AM
* @version 1.0
**/
public class JsonHelper {
/**
* 把javaBean对象转为Map
* @param javaBean
* @return
*/
public static Map beanToMap(Object javaBean) {
Map map = new HashMap();
Method [] methods = javaBean.getClass().getDeclaredMethods();
for(Method method : methods){
try {
if(method.getName().startsWith("get")) {
String field = method.getName();
field = field.substring(field.indexOf("get") + 3);
field = field.toLowerCase().charAt(0) + field.substring(1);
Object value = method.invoke(javaBean, (Object[]) null);
map.put(field, null == value ? "" : value.toString());
}
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
}
}
return map;
}
/**
* 将json对象转换成map
* @param jsonString
* @return
* @throws JSONException
*/
public static Map jsonToMap(String jsonString) throws JSONException {
JSONObject jsonObject = new JSONObject(jsonString);
Map result = new HashMap();
Iterator iterator = jsonObject.keys();
String key = null;
String value = null;
while(iterator.hasNext()){
key = (String)iterator.next();
value = jsonObject.getString(key);
result.put(key, value);
}
return result;
}
/**
* 将javaBean转为json(通过map中转)
* @param bean
* @return
*/
public static JSONObject beanToJson(Object bean) {
return new JSONObject(beanToMap(bean));
}
/**
* 将map转为bean
* @param bean
* @param data
* @return
*/
public static Object mapToBean(Object bean, Map data) {
Method[] methods = bean.getClass().getDeclaredMethods();
for (Method method : methods) {
try {
if (method.getName().startsWith("set")) {
String field = method.getName();
field = field.substring(field.indexOf("set" + 3));
field = field.toLowerCase().charAt(0) + field.substring(1);
method.invoke(bean, new Object[]{
data.get(field)
});
}
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bean;
}
/**
* json数据转为bean
* @param bean
* @param jsonString
* @throws JSONException
*/
public static void jsonToBean(Object bean, String jsonString) throws JSONException{
JSONObject jsonObject = new JSONObject(jsonString);
Map map = jsonToMap(jsonObject.toString());
mapToBean(bean, map);
}
}
JsonTest.java
package com.json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* 测JsonHelper的测试类
* @author xiejunbo
* @email 624664181@qq.com
* @date Nov 17, 2014 1:11:20 AM
* @version 1.0
**/
public class JsonTest {
/**
* 构造json数据
* @return
* @throws JSONException
*/
public static String BuildJson() throws JSONException {
//json格式数据解析对象
JSONObject jo = new JSONObject();
//以下构造两个map、一个list和一个User对象
Map map1 = new HashMap();
map1.put("userName", "xiejunbo");
map1.put("password", "a123456677");
map1.put("email", "624664181@qq.com");
Map map2 = new HashMap();
map2.put("name", "谢");
map2.put("password", "ASFFS");
map2.put("email", "23@xx.com");
List list = new ArrayList();
list.add(map1);
list.add(map2);
User user = new User();
user.setUserName("jun");
user.setPassword("1234567890");
user.setEmail("asdf@qq.com");
//将map转为JSONArray数据
JSONArray ja = new JSONArray();
ja.put(map1);
System.out.println("JSONArray对象数据格式:");
System.out.println(ja.toString());
//将java bean转为json数据(需Map中转)
JSONObject jo1 = JsonHelper.beanToJson(user);
System.out.println("仅含user对象的Json数据");
System.out.println(jo1.toString());
//构造一个json数据,包括一个map和一个含user对象的json数据
jo.put("map", ja);
jo.put("user", jo1.toString());
System.out.println("最终构造的json数据格式:");
System.out.println(jo.toString());
return jo.toString();
}
/**
* 解析json数据
* @param args
* @throws JSONException
*/
public static void ParseJson(String jsonString) throws JSONException {
JSONObject jo = new JSONObject(jsonString);
JSONArray ja = jo.getJSONArray("map");
System.out.println("将json数据解析为map:");
System.out.println("userName:" + ja.getJSONObject(0).getString("userName")
+ " password:" + ja.getJSONObject(0).getString("password")
+" email:" + ja.getJSONObject(0).getString("email"));
String jsonStr = jo.getString("user");
User user = new User();
JsonHelper.jsonToBean(user, jsonStr);
System.out.println("将json数据解析为user对象:");
System.out.println("userName:" + user.getUserName()
+ " password:" + user.getPassword()
+ " email:" + user.getEmail());
}
/**
*
* @param args
* @throws JSONException
*/
public static void main(String args[]) throws JSONException {
ParseJson(BuildJson());
}
}
User.java
package com.json;
/**
* Bean
* @author xiejunbo
* @email 624664181@qq.com
* @date Nov 17, 2014 1:12:33 AM
* @version 1.0
**/
public class User {
private String userName;
private String password;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
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;
}
}