java中处理JSON的开源工具都有些啥?那个比较好用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中处理JSON的开源工具都有些啥?那个比较好用相关的知识,希望对你有一定的参考价值。

我用过的有jackson,谷歌的gson,阿里的fastjson
fastjson在遍历JSONObject的时候顺序不是有序的,想要有序还得改源码,比较麻烦,序列化对象时属性的顺序也不能保证,是我放弃使用的主要原因
gson是用起来是最痛快的,一句话就能解决

12

String string = new Gson().toJson(user);User user = new Gson().fromeJson(string, User.class);

特点是不报错,如果输入的json字符串和Java Bean的属性对应不上,相应属性会为null或默认值这是优点还是缺点就见仁见智了,至少我在转换一些不太规则的API返回值时还是挺方便的。
参考技术A fastjson, jackson这两个不错。

带有function的JSON对象的序列化与还原

参考技术A JSON对象的序列化与反序列化相信大家都很熟悉了。基本的api是JSON.parse与JSON.stringify.

ok 到现在为止都没啥问题,处理得很好,但是现在我有这么一个json对象

我们看下JSON.stringify(json)输出啥

""name":"json""

把getName弄丢了 ,怎么办呢?其实大家都没注意到JSON.stringify还有些参数

Required. A JavaScript value, usually an object or array, to be converted.

Optional. A function or array that transforms the results.

If replacer is a function, JSON.stringify calls the function, passing in the key and value of each member. The return value is used instead of the original value. If the function returns undefined, the member is excluded. The key for the root object is an empty string: "".

If replacer is an array, only members with key values in the array will be converted. The order in which the members are converted is the same as the order of the keys in the array. The replacer array is ignored when thevalue argument is also an array.

Optional. Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

If space is omitted, the return-value text is generated without any extra white space.

If space is a number, the return-value text is indented with the specified number of white spaces at each level. Ifspace is greater than 10, text is indented 10 spaces.

If space is a non-empty string, such as '\t', the return-value text is indented with the characters in the string at each level.

If space is a string that is longer than 10 characters, the first 10 characters are used.
那我们现在就可以把函数也序列化了

ok现在我们已经成功的序列化带function的json对象了,接下来如何还原它呢?

Required. A valid JSON string.

Optional. A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is. For each member, the following occurs:
If reviver returns a valid value, the member value is replaced with the transformed value.
If reviver returns the same value it received, the member value is not modified.
If reviver returns null or undefined, the member is deleted.

那么我们就可以这么来还原json对象

以上是关于java中处理JSON的开源工具都有些啥?那个比较好用的主要内容,如果未能解决你的问题,请参考以下文章

java中处理JSON的开源工具都有些啥?那个比较好用

java中处理JSON的开源工具都有些啥?那个比较好用

latex支持的文档格式都有哪些?支持的图片格式都有哪些?有些啥编译方式?

AngularJS搭配啥后端框架比较合适

重学springboot系列之JSON处理工具类

啥报表工具可以支持动态的数据源