设置表达式指定位置(或field对应)的值
若表达式指向一个JSONArray则设置其坐标对应位置的值,若指向JSONObject则put对应key的值
注意:如果为JSONArray,设置值下标小于其长度,将替换原有值,否则追加新值
.表达式,可以获取Bean对象中的属性(字段)值或者Map中key对应的值
[]表达式,可以获取集合等对象中对应index的值
表达式栗子:
persion
persion.name
persons[3]
person.friends[5].name
Specified by:
putByPath
in interface
JSON
Parameters:
expression
- 表达式
value
- 值
Object
value)
throws
JSONException
Deprecated.
PUT 键值对到JSONObject中,在忽略null模式下,如果值为
null
,将此键移除
Specified by:
put
in interface
Map
<
String
,
Object
>
Overrides:
put
in class
MapWrapper
<
String
,
Object
>
Parameters:
key
- 键
value
- 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
Returns:
this.
Throws:
JSONException
- 值是无穷数字抛出此异常
Parameters:
key
- 键
value
- 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
Returns:
this.
Throws:
JSONException
- 值是无穷数字抛出此异常
public JSONObject set(String key,
Object value,
Filter<MutablePair<String,Object>> filter,
boolean checkDuplicate)
throws JSONException
设置键值对到JSONObject中,在忽略null模式下,如果值为
null
,将此键移除
Parameters:
key
- 键
value
- 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
filter
- 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,
null
表示不过滤
checkDuplicate
- 是否检查重复键,如果为
true
,则出现重复键时抛出
JSONException
异常
Returns:
this.
Throws:
JSONException
- 值是无穷数字抛出此异常
Since:
5.8.0
Parameters:
key
- 键
value
- 值对象,可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
Returns:
this.
Throws:
JSONException
- 值是无穷数字、键重复抛出异常
public JSONObject setOnce(String key,
Object value,
Filter<MutablePair<String,Object>> filter)
throws JSONException
一次性Put 键值对,如果key已经存在抛出异常,如果键值中有null值,忽略
Parameters:
key
- 键
value
- 值对象,可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
filter
- 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,
null
表示不过滤
Returns:
Throws:
JSONException
- 值是无穷数字、键重复抛出异常
Since:
5.8.0
Parameters:
key
- 键
value
- 值对象,可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
Returns:
this.
Throws:
JSONException
- 值是无穷数字
throws
JSONException
积累值。类似于set,当key对应value已经存在时,与value组成新的JSONArray.
如果只有一个值,此值就是value,如果多个值,则是添加到新的JSONArray中
Parameters:
key
- 键
value
- 被积累的值
Returns:
this.
Throws:
JSONException
- 如果给定键为
null
或者键对应的值存在且为非JSONArray
Object
value)
throws
JSONException
追加值,如果key无对应值,就添加一个JSONArray,其元素只有value,如果值已经是一个JSONArray,则添加到值JSONArray中。
Parameters:
key
- 键
value
- 值
Returns:
this.
Throws:
JSONException
- 如果给定键为
null
或者键对应的值存在且为非JSONArray
public JSONObject increment(String key)
throws JSONException
对值加一,如果值不存在,赋值1,如果为数字类型,做加一操作
Parameters:
key
- A key string.
Returns:
this.
Throws:
JSONException
- 如果存在值非Integer, Long, Double, 或 Float.
toJSONString
public String toJSONString(int indentFactor,
Filter<MutablePair<Object,Object>> filter)
返回JSON字符串
支持过滤器,即选择哪些字段或值不写出
Parameters:
indentFactor
- 每层缩进空格数
filter
- 过滤器,同时可以修改编辑键和值
Returns:
JSON字符串
Since:
5.7.15
Description copied from interface:
JSON
将JSON内容写入Writer
Warning: This method assumes that the data structure is acyclical.
Specified by:
write
in interface
JSON
Parameters:
writer
- writer
indentFactor
- 缩进因子,定义每一级别增加的缩进量
indent
- 本级别缩进量
Returns:
Writer
Throws:
JSONException
- JSON相关异常