Java解析Jsonデータ、JavaタイプとJsonデータ型の変換
6500 ワード
Java解析Jsonデータ、JavaタイプとJsonデータ型の変換:
使用するjarファイル:json.org.jar
使用する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 [email protected]
* @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 [email protected]
* @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<String, String> map1 = new HashMap<String, String>();
map1.put("userName", "xiejunbo");
map1.put("password", "a123456677");
map1.put("email", "[email protected]");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("name", " ");
map2.put("password", "ASFFS");
map2.put("email", "[email protected]");
List<Map> list = new ArrayList<Map>();
list.add(map1);
list.add(map2);
User user = new User();
user.setUserName("jun");
user.setPassword("1234567890");
user.setEmail("[email protected]");
// 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 [email protected]
* @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;
}
}