2019独角兽企业重金招聘Python工程师标准>>>

package test;import cn.hutool.core.lang.Console;
import cn.jiguang.entity.ErrorMessage;
import com.alibaba.fastjson.JSON;
import com.dslplatform.json.DslJson;
import com.dslplatform.json.JsonReader;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jsoniter.JsonIterator;
import com.jsoniter.ValueType;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.Assert;import java.io.*;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;/*** @desc: dsl-json  fastjson  jackson  jsoniter 效率比较* @author: zengxc* @date: 2018/4/4*/
public class JSONTest {private static final ObjectMapper MAPPER = new ObjectMapper();public static String[] factors = {};private static String JSON_STR = null;private static int count = 0;private final DslJson<Object> dslJson = new DslJson<Object>();@Testpublic void testJsoniter2() throws IOException {JsonIterator jsonIterator = JsonIterator.parse("[123,{'id':20, 'errorMsg':'this error'},456]".replace('\'', '"'));AtomicReference<ErrorMessage> errorMessage = new AtomicReference<>();jsonIterator.readAny().forEach(any -> {if (any.valueType() == ValueType.OBJECT) {errorMessage.set(any.as(ErrorMessage.class));}});Console.print(errorMessage.get());Assert.isTrue(true, "Not to false");}@Testpublic void testJsoniter() throws IOException {JsonIterator jsonIterator = JsonIterator.parse("{'id':10, 'errorMsg':'this error'}".replace('\'', '"'));ErrorMessage errorMessage = jsonIterator.read(ErrorMessage.class);Assert.notNull(errorMessage, "can't be empty ");}@Testpublic void testFor() throws IOException {File file = new File("out.txt");file.mkdirs();PrintWriter printWriter = new PrintWriter(file);StringBuffer buffer = new StringBuffer();buffer.append("{");for (int i = 0; i < 1000000; i++) {if (i == 999999) {buffer.append("\"k" + i + "\":" + "\"v" + i + "\"" + "}");break;}buffer.append("\"k" + i + "\":" + "\"v" + i + "\"" + ",");}printWriter.println(buffer.toString());printWriter.flush();}@Beforepublic void init() throws Exception {FileReader fis = new FileReader(new File("D:\\WorkSpace\\springboot-helloworld\\out.txt"));BufferedReader br = new BufferedReader(fis);JSON_STR = br.readLine();}@Testpublic void testFastJson() {long startTime = System.currentTimeMillis();Map<String, String> map = JSON.parseObject(JSON_STR, Map.class);map.forEach((k, v) -> {count++;});System.out.println(System.currentTimeMillis() - startTime);System.out.println(count);}@Testpublic void testJsoniter03() throws IOException {long startTime = System.currentTimeMillis();JsonIterator jsonIterator = JsonIterator.parse(JSON_STR);Map<String, String> map = jsonIterator.readAny().as(Map.class);map.forEach((k, v) -> {count++;});System.out.println(System.currentTimeMillis() - startTime);System.out.println(count);}@Testpublic void testJackson() throws IOException {long startTime = System.currentTimeMillis();Map<String, String> map = MAPPER.readValue(JSON_STR, Map.class);map.forEach((k, v) -> {count++;});System.out.println(System.currentTimeMillis() - startTime);System.out.println(count);}@Testpublic void testDslJson() throws IOException {long startTime = System.currentTimeMillis();final byte[] buff = JSON_STR.getBytes("UTF-8");JsonReader<Object> jsonReader = dslJson.newReader(buff);Map map = jsonReader.next(Map.class);map.forEach((k, v) -> {count++;});System.out.println(System.currentTimeMillis() - startTime);System.out.println(count);}
}

结论:

dslJson > jsonIter > fastJson > jackson

转载于:https://my.oschina.net/u/3744350/blog/1790116

dslJson、jsoniter、fastjson、jackson解析字符串为map效率比较相关推荐

  1. 用 FastJSON 将 JSON 字符串转换为 Map

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 本文是利用阿里巴巴封装的FastJSON来转换json字符串的.例子如下: import com.a ...

  2. [转]jackson json字符串、map、java对象的转换例子

    ackson框架 json字符串.map.java对象的转换例子 先下载框架jar包: 下面是一些例子: package jackson; import java.io.File; import ja ...

  3. fastjson将json字符串转为Map对象,拿走不谢

    废话少说直接上代码 Map<String, Object> map = JSON.parseObject(result, new TypeReference<Map<Strin ...

  4. java代码中fastjson生成字符串和解析字符串的方法和javascript文件中字符串和json数组之间的转换方法...

    1.java代码中fastjson生成字符串和解析字符串的方法 List<TemplateFull> templateFulls = new ArrayList<TemplateFu ...

  5. 记一次FastJSON和Jackson解析json时遇到的中括号问题

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jadyer/article/details/24395015 完整版见https://jadyer. ...

  6. Jackson - 将 JSON 字符串转换为 Map

    ① pom依赖: <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId&g ...

  7. C# json解析字符串总是多出双引号_json从立地到成佛

    本文原创作者bigsai(同公众号),本文以浅显易懂的方式给大家讲解自己所认知的json,如果有错误或者不准确地方还请大家给出指正,另外本文以关卡课程的方式在博学谷也是免费开放的,大家也可通过关卡方式 ...

  8. JSON字符串转换为Map

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 本文是利用阿里巴巴封装的FastJSON来转换json字符串的.例子如下: [java] view ...

  9. FastJson之json字符串与javabean的互相转换

    1.基本使用说明 FastJson常用到一下三个类: (1)JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换. (2)JSONObject:fast ...

最新文章

  1. JS怎么刷新当前页面
  2. 怎么获取排他网关下的所有userTask?
  3. 服务拆分-服务远程调用
  4. 多线程中,NSOperationQueue和GCD的区别
  5. 试图加载 Crystal Reports 运行时出现错误
  6. Lomboz插件的使用
  7. idea破解码(最后一个有用)
  8. 区块链开发入门教程推荐
  9. 企业微信oauth认证_企业微信登陆
  10. mysql 空格键和回车键查询
  11. 【软件工程/系统软件/程序设计语言】 2019年-中国计算机学会推荐国际学术会议和期刊目录(四)
  12. 2021年中国果蔬汁行业供需分析:产量同比增长2.4%[图]
  13. 【Unity开发小技巧】Unity组合键的代码编写
  14. Web 前端通过调用ActiveX实现LPT1端口小票机打印功能。
  15. 原来等待我做的事情还有很多
  16. 基于SSM的超市会员管理系统
  17. tikz 折线 箭头_[LaTeX 绘图] tikz 绘制流程图,概述和两个示例
  18. 最短路迪杰斯特拉回炉重造
  19. 2011年系统架构师考试题详解
  20. Keras.layers.core.dense()方法详解

热门文章

  1. 毕业设计 c语言编译器的设计开发-字节代码格式设计与实现 开题报告,C语言编译器设计与实现...
  2. 怎样找出插件的api地址_百度云盘免登陆高速下载,调用度云简易分享地址提取在线工具...
  3. html 页面宽度和背景颜色,CSS:设置背景颜色为窗口宽度的50%
  4. 时间立即同步命令_Redis复制:主从同步
  5. 科技边框_智能手机窄边框喷射点胶机欧力克斯
  6. inet_ntop php,inet_ntop()
  7. for循环递减_判断语句_循环语句
  8. PHP 接收 UDP包_PHP早已不是十年前的鸟样!!!
  9. Debugging JTAG
  10. 【FPGA】ROM/EPROM的设计(使用case的方式初始化)