java实现,json嵌套json
Map<String, String> predata = new HashMap<String, String>();
Map<String, Object> returnCode = new HashMap<String, Object>();
predata.put(“state”, “正常”);
predata.put(“state1”, “超分”);
predata.put(“state2”, “超分2”);
predata.put(“state3”, “超分3”);
predata.put(“state4”, “超分4”);
returnCode.put(“returnMsg”, “异常”);
returnCode.put(“returnMsg2”, “异常2”);
returnCode.put(“returnMsg3”, “异常3”);
String msgData = JacksonUtils.serialObject(predata);
returnCode.put(“info”, predata);
String msgew = JacksonUtils.serialObject(returnCode);
System.out.println(msgew );
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;
/**
*
*
*
*/
public class JacksonUtils {
/**
*
* @param val
* @param cls
* @return
* @throws JsonParseException
* @throws IOException
*/
public static <T> T parseJsonFromString(String val, Class<T> cls) throws JsonParseException, IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper.readValue(val, cls);
}
/**
*
* @param obj
* @param filters
* @return
* @throws JsonProcessingException
*/
public static String serialObject(Object obj, FilterProvider filters) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
if (filters != null) {
mapper.setFilters(filters);
}
return mapper.writeValueAsString(obj);
}
/**
*
* @param val
* @return
* @throws JsonParseException
* @throws IOException
*/
public static JsonNode parseJsonFromString(String val) throws JsonParseException, IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readTree(val);
}
/**
*
* @param obj
* @return
* @throws JsonProcessingException
*/
public static String serialObject(Object obj) throws JsonProcessingException {
return serialObject(obj, null);
}
}