com.datastax.util.web.JSONUtil Maven / Gradle / Ivy
The newest version!
package com.datastax.util.web;
import com.alibaba.fastjson.JSON;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Anders Hong 6/24/2015.
*/
public class JSONUtil {
public static HashMap jsonToMap(String json){
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
TypeReference> typeRef
= new TypeReference>() {};
try {
return mapper.readValue(json, typeRef);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static List jsonToList(String json){
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
TypeReference> typeRef
= new TypeReference>() {};
try {
return mapper.readValue(json, typeRef);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static Object find(HashMap map,String path){
String[] nodes=path.split("/");
for(int i=0;i)map.get(node);
}
}
return null;
}
public static Object findAdvance(HashMap map,String path){
String[] nodes=path.split("/");
Object o=map.get(nodes[0]);
for(int i=1;i T getEntity(String jsonString, Class clazz){
return JSON.parseObject(jsonString,clazz);
}
public static T getEntity(String json, com.alibaba.fastjson.TypeReference type){
return JSON.parseObject(json,type);
}
public static List parseList(String json,Class clazz){
return JSON.parseArray(json,clazz);
}
public static T[] parseArray(String json,Class clazz){
List list=parseList(json,clazz);
T[] ts= (T[])Array.newInstance(clazz,list.size());
ts=list.toArray(ts);
return ts;
}
}