package test;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.mortbay.util.ajax.JSON;
import java.util.*;
public class FastJSONUtils {
public static void main(String[] args) {
Map iotMsg = new HashMap<String, Map<String,Map<String,Object>>>();
Map headMsg = new HashMap();
headMsg.put("method","iotmon");
headMsg.put("gw_id","pg");
headMsg.put("sensor_id","11");
headMsg.put("msg_time",new Long(1531105889464L));
headMsg.put("msg_id",new Integer(256));
headMsg.put("version","1.0");
iotMsg.put("head",headMsg);
// Set set = headMsg.keySet();
// Collection values = headMsg.values();
// System.out.println(set.toString());
// System.out.println(values.toString());
// Object head = iotMsg.get("head");
// System.out.println(head);
Map inspectMsg = new HashMap<String,Object>();
inspectMsg.put("f0",new String("14.6,10.8,12.4,38.6,39.4,58.2,58.24,394.4,375,304.4"));
Map<String,Map<String, Object>> bodyMsg = new HashMap<String, Map<String, Object>>();
bodyMsg.put("inspect",inspectMsg);
iotMsg.put("body",bodyMsg);
String str_json = FastJSONUtils.convertMapToJson(iotMsg);
System.out.println("str_json :" + str_json);
String head = FastJSONUtils.getJsonString("head", str_json);
//System.out.println(FastJSONUtils.getJsonString("sensor_id","head"));//会报错,必须转成map才能取出key的value值
Map<String, Object> map = FastJSONUtils.parseJsonToMap(head);
System.out.println("map对象是:" + map);
System.out.println(map.get("method"));
String body = FastJSONUtils.getJsonString("body", str_json);
String inspect = FastJSONUtils.getJsonString("inspect", body);
System.out.println(inspect);
// String f0 = FastJSONUtils.getJsonString("f0", "inspect");
Map<String, Object> mapinsp = FastJSONUtils.parseJsonToMap(inspect);
System.out.println("mapinsp对象是:" + mapinsp);
System.out.println(mapinsp.get("f0"));
}
/**
* 将Map转换成json
* @param map
* @author dd123
* @date 2020-6-13
*/
public static String convertMapToJson(Map<String, Object> map){
return JSON.toString(map);
}
/**
* 将Object转换成json
* @param obj
* @author dd123
* @date 2020-3-12
*/
public static String convertObjToJson(Object obj,SerializerFeature feature){
return com.alibaba.fastjson.JSON.toJSONString(obj,feature);
}
/**
* 将Object转换成json
* @param obj
* @author lihan
* @date 2019-3-12
*/
public static String convertObjToJson(Object obj){
return com.alibaba.fastjson.JSON.toJSONString(obj);
}
/**
* 获取json的类型
* @param json
* @author dd123
* @date 2021-2-13
*/
public static JsonType getJSONType(String json) {
/** if (StringUtils.isEmpty(json)) {
return JsonType.JSON_TYPE_ERROR;
}
**/
final char[] strChar = json.substring(0, 1).toCharArray();
final char firstChar = strChar[0];
if (firstChar == '{') {
return JsonType.JSON_TYPE_OBJECT;
} else if (firstChar == '[') {
return JsonType.JSON_TYPE_ARRAY;
} else {
return JsonType.JSON_TYPE_ERROR;
}
}
/**
* 获取指定key的值
* @param json
* @author wangyl
* @date 2021-2-13
*/
public static String getJsonString(String key, String json) {
JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(json);
return jsonObject.getString(key);
}
/**
* 获取指定key的值
* @param json
* @author dd123
* @date 2019-6-13
*/
public static int getJsonInt(String key, String json) {
JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(json);
return jsonObject.getInteger(key);
}
/**
* 获取指定key的值
* @param json
* @author dd123
* @date 2019-6-13
*/
public static long getJsonLong(String key, String json) {
JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(json);
return jsonObject.getLongValue(key);
}
/**
* 将json解析成map
* @author dd123
* @date 2020-6-13
*/
//@SuppressWarnings("unchecked")
public static Map<String,Object> parseJsonToMap(String mapJson){
return (Map<String,Object>)JSONObject.parse(mapJson);
}
public enum JsonType {
JSON_TYPE_OBJECT,
JSON_TYPE_ARRAY,
JSON_TYPE_ERROR
}
}