import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import ykt.api.Result;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
/**
* Json工具类
*
* @author longtj
*
*/
public class JsonUtil {
/**
* 获取json字符串
* @param object
*/
public static String object2JsonString(Object object) {
String jsonString = "";
try {
jsonString = JSON.toJSONString(object);
} catch (Exception e) {
e.printStackTrace();
}
return jsonString;
}
/**
* 获取解析json
*
* @param resultJsonString json字符串
* @param codeText codeText实例 接收返回code 和 text信息
* @return list 返回的list消息
*/
public static List<?> parseJson(String resultJsonString,
Map<String, Object> codeText) {
List<Map<String, Object>> maps;
JSONObject jsonObject;
try {
jsonObject = JSON.parseObject(resultJsonString);
codeText.put("code", jsonObject.getString("code"));
codeText.put("text", jsonObject.getString("text"));
JSONArray jsonArray = jsonObject.getJSONArray("object");
maps = new ArrayList<Map<String, Object>>();
if (jsonArray != null && jsonArray.size() > 0) {
for (int i = 0; i < jsonArray.size(); i++) {
String jsonObjectString = jsonArray.getJSONObject(i)
.toJSONString();
Map<String, Object> obj = JSON.parseObject(
jsonObjectString,
new TypeReference<Map<String, Object>>() {
});// 转换为map
maps.add(obj);
}
}
return maps;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
List<Map<String, Object>> xx = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("xx", "xx");
xx.add(map);
Result r = new Result();
r.setCode("1");
r.setText("成功");
r.setObject(xx);
String xxx = object2JsonString(r);
Map<String, Object> codeText = new HashMap<String, Object>();
List<?> xxxxx = parseJson(xxx, codeText);
// System.out.println(xx);
System.out.println(xxxxx);
System.out.println(codeText);
}
/**
* 将Json对象转换成Map
* @param jsonObject json对象
* @return Map对象
* @throws JSONException
* @throws org.json.JSONException
*/
public static Map<String, Object> toMap(String jsonString)
throws JSONException, org.json.JSONException {
org.json.JSONObject jsonObject = new org.json.JSONObject(jsonString);
Map<String, Object> result = new HashMap<String, Object>();
@SuppressWarnings("unchecked")
Iterator<Object> 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转换为Map
* @param javaBean javaBean
* @return Map对象
*/
public static Map<String, Object> toMap(Object javaBean) {
Map<String, Object> result = new HashMap<String, Object>();
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);
result.put(field, null == value ? "" : value.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 将JavaBean转换成JSONObject(通过Map中转)
* @param bean javaBean
* @return json对象
*/
public static JSONObject toJSON(Object bean) {
return new JSONObject(toMap(bean));
}
/**
* 将Map转换成Javabean
*
* @param javabean javaBean
* @param data Map数据
*/
public static Object toJavaBean(Object javabean, Map<String, Object> data) {
Method[] methods = javabean.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(javabean, new Object[] { data.get(field) });
}
} catch (Exception e) {
}
}
return javabean;
}
/**
* JSONObject到JavaBean
* @param bean javaBean
* @return json对象
* @throws ParseException json解析异常
* @throws JSONException
* @throws org.json.JSONException
*/
public static void toJavaBean(Object javabean, String jsonString)
throws ParseException, org.json.JSONException {
org.json.JSONObject jsonObject = new org.json.JSONObject(jsonString);
Map<String, Object> map = toMap(jsonObject.toString());
toJavaBean(javabean, map);
}
/**
* json数组转换为List<Map<String, Object>>
* @param JSONArray类型字符串
* @return List<Map<String, Object>>
* @throws org.json.JSONException
*/
public static List<Map<String, Object>> jsonArray2ListMap(String arrays) throws org.json.JSONException {
org.json.JSONArray array=new org.json.JSONArray(arrays);
int size=array.length();
List<Map<String, Object>> list=null;
if(size>0){
list=new ArrayList<Map<String,Object>>();
for (int i = 0; i < array.length(); i++) {
String res=array.getString(i);
System.out.println(JsonUtil.toMap(res));
list.add(JsonUtil.toMap(array.getString(i)));
}
}
return list;
}
}
json 解析
最新推荐文章于 2022-10-09 11:57:21 发布