在其他博客上看到的方法
SerializeConfig serializeConfig =new SerializeConfig();
serializeConfig.put(Number.class, ToStringSerializer.instance);
serializeConfig.put(Long.class,ToStringSerializer.instance);
serializeConfig.put(Long.TYPE,ToStringSerializer.instance);
serializeConfig.put(BigDecimal.class,ToStringSerializer.instance);
String s = JSONObject.toJSONString(jsonObject, serializeConfig, SerializerFeature.PrettyFormat);
JSON.parseObject(s);
我觉得这个方法太费性能,自己写了一个类,然后对比了一下,下面是两种方法的对比,可以看看,耗时应该是大幅度降低了的。
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Map;
public class JsonUtils {
public static <T> T setNumberValueToString(T json){
if(json instanceof JSONObject){
JSONObject newJSON = new JSONObject();
JSONObject jsonObject =(JSONObject) json;
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
Object value = entry.getValue();
if(value instanceof Number){
newJSON.put(entry.getKey(),entry.getValue().toString());
}
else if(value instanceof JSONObject){
newJSON.put(entry.getKey(),setNumberValueToString(value));
}
else if(value instanceof JSONArray){
newJSON.put(entry.getKey(),setNumberValueToString(value));
}else {
newJSON.put(entry.getKey(),entry.getValue());
}
}
return (T) newJSON;
}
if(json instanceof JSONArray){
JSONArray newJSONArray = new JSONArray();
JSONArray valueArray =(JSONArray) json;
for (Object o : valueArray) {
newJSONArray.add(setNumberValueToString(o));
}
return (T) newJSONArray;
}
if(json instanceof Number){
return (T) json.toString();
}
return json;
}
public static void main(String[] args) {
SerializeConfig serializeConfig =new SerializeConfig();
serializeConfig.put(Number.class, ToStringSerializer.instance);
serializeConfig.put(Long.class,ToStringSerializer.instance);
serializeConfig.put(Long.TYPE,ToStringSerializer.instance);
serializeConfig.put(BigDecimal.class,ToStringSerializer.instance);
serializeConfig.put(Integer.class,ToStringSerializer.instance);
JSONObject jsonObject= JSONObject.parseObject("{\n" +
" \"min_position\": 7,\n" +
" \"has_more_items\": true,\n" +
" \"items_html\": \"Car\",\n" +
" \"new_latent_count\": 1,\n" +
" \"data\": {\n" +
" \"length\": 21,\n" +
" \"text\": \"xixingya\"\n" +
" },\n" +
" \"numericalArray\": [\n" +
" 31,\n" +
" 23,\n" +
" 24,\n" +
" 29,\n" +
" 33\n" +
" ],\n" +
" \"StringArray\": [\n" +
" \"Nitrogen\",\n" +
" \"Carbon\",\n" +
" \"Nitrogen\",\n" +
" \"Nitrogen\"\n" +
" ],\n" +
" \"multipleTypesArray\": 3,\n" +
" \"objArray\": [\n" +
" {\n" +
" \"class\": \"middle\",\n" +
" \"age\": 8\n" +
" },\n" +
" {\n" +
" \"class\": \"upper\",\n" +
" \"age\": 9\n" +
" },\n" +
" {\n" +
" \"class\": \"lower\",\n" +
" \"age\": 7\n" +
" },\n" +
" {\n" +
" \"class\": \"middle\",\n" +
" \"age\": 7\n" +
" },\n" +
" {\n" +
" \"class\": \"middle\",\n" +
" \"age\": 5\n" +
" }\n" +
" ]\n" +
"}");
long current = System.currentTimeMillis();
//FastJson转换
for (int i = 0; i < 100000; i++) {
String s = JSONObject.toJSONString(jsonObject, serializeConfig, SerializerFeature.PrettyFormat);
JSON.parseObject(s);
}
System.out.println("use time ="+(System.currentTimeMillis()-current));
current = System.currentTimeMillis();
//Util转换
for (int i = 0; i < 100000; i++) {
JsonUtils.setNumberValueToString(jsonObject);
}
System.out.println("use time ="+(System.currentTimeMillis()-current));
String s = JSONObject.toJSONString(jsonObject, serializeConfig,SerializerFeature.PrettyFormat);
JSONObject jsonObject1 = JSON.parseObject(s);
System.out.println(jsonObject1);
System.out.println(JsonUtils.setNumberValueToString(jsonObject));
}
}