public static void main(String[] args) {
//构建模拟json数据
JSONObject json = new JSONObject();
for (int i = 1; i = 5; i++) {
json.put("key"+i, "value"+i);
}
System.out.println("json===="+json.toString());
//解析json
MapString,Object map = new HashMapString,Object();
for (String obj : json.keySet()) {
map.put(obj, json.get(obj));
}
System.out.println("map===="+map.toString());
}
最好在这边try catch下,防止出现异常!
这个是循环引用 防止内存溢出 可以关闭
JSON.toJSONString(..., SerializerFeature.DisableCircularReferenceDetect)
/**
* json特殊操作
* p
*
* @author 宋汝波
* @date 2014年11月24日
* @version 1.0.0
*/
public class JsonUtil {
private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);
/**
* 对序列化的Long类型进行特殊处理,避免位数过大导致和js精度的丢失,只用于向页面发送json数据时使用
*/
static ObjectSerializer longSerializer = new ObjectSerializer() {
@Override
public void write( JSONSerializer serializer, Object object, Object fieldName, Type fieldType )
throws IOException {
SerializeWriter out = serializer.getWriter();
if ( object == null ) {
if ( out.isEnabled(SerializerFeature.WriteNullNumberAsZero) ) {
out.write('0');
} else {
out.writeNull();
}
return;
}
out.writeString(object.toString());
}
};
/**
* 对Long型兼容js的json串
* p
*
* @param object
* 对象
* @return json字符串
*/
public static final String toCompatibleJSON( Object object, String format ) {
SerializeWriter out = new SerializeWriter();
try {
//此处必须new一个SerializeConfig,防止修改默认的配置
JSONSerializer serializer = new JSONSerializer(out, new SerializeConfig());
serializer.getMapping().put(Long.class, longSerializer);
if ( format != null ) {
serializer.getMapping().put(Date.class, new SimpleDateFormatSerializer(format));
}
serializer.write(object);
return out.toString();
} finally {
out.close();
}
}
public static final String toCompatibleJSON( Object object ) {
return toCompatibleJSON(object, null);
}
public static void main( String[] args ) {
logger.debug(toCompatibleJSON(new Date(), "yyyy-MM-dd"));
logger.debug(toCompatibleJSON(new Date(), null));
}
}
我来说两句