Java -fastjson api
构造 json 对象
需求:构造以下请求体
{
"attrSelectionVO": [
{
"attrAccessId": "eea99a0894504a2b89f3cfeb4be051d3",
"attrValueList": [
{
"attrValue": "输送型",
"attrValueAccessId": "52d54de77b224cc8b5f9af647c938b10",
}
],
"attributeName": "皮带形状",
"constraintCondition": 1,
"mergedRangeParamValue": null,
"paramName": null,
"paramValue": null,
"selectionSort": 2,
"type": 0
}
],
"categoryCode": "C02"
}
实现方式一:
@Test
public void test01(){
JSONObject requBody = new JSONObject();
JSONArray attrSelectionVOArray = new JSONArray();
requBody.put("attrSelectionVO", attrSelectionVOArray);
requBody.put("categoryCode","C02");
JSONObject attrSelectionVOEle = new JSONObject();
attrSelectionVOEle.put("attrAccessId", "eea99a0894504a2b89f3cfeb4be051d3");
JSONArray attrValueListArray = new JSONArray();
JSONObject attrValueListEle = new JSONObject();
attrValueListEle.put("attrValue", "输送型");
attrValueListEle.put("attrValueAccessId", "52d54de77b224cc8b5f9af647c938b10");
attrValueListArray.add(attrValueListEle);
attrSelectionVOEle.put("attrValueList", attrValueListArray);
attrSelectionVOEle.put("attributeName", "皮带形状");
attrSelectionVOEle.put("constraintCondition", 1);
attrSelectionVOEle.put("mergedRangeParamValue", null);
attrSelectionVOEle.put("paramName", "null");
attrSelectionVOEle.put("paramValue", "null");
attrSelectionVOEle.put("type", 0);
attrSelectionVOArray.add(attrSelectionVOEle);
System.out.println(requBody.toJSONString());
}
实现方式二:
@Test
public void test02(){
JSONObject requBody = new JSONObject();
JSONArray attrSelectionVOArray = requBody.putArray("attrSelectionVO");
requBody.put("categoryCode","C02");
JSONObject attrSelectionVOEle = attrSelectionVOArray.addObject();
attrSelectionVOEle.put("attrAccessId", "eea99a0894504a2b89f3cfeb4be051d3");
JSONArray attrValueListArray = attrSelectionVOEle.putArray("attrValueList");
JSONObject attrValueListEle = attrValueListArray.addObject();
attrValueListEle.put("attrValue", "输送型");
attrValueListEle.put("attrValueAccessId", "52d54de77b224cc8b5f9af647c938b10");
attrSelectionVOEle.put("attributeName", "皮带形状");
attrSelectionVOEle.put("constraintCondition", 1);
attrSelectionVOEle.put("mergedRangeParamValue", null);
attrSelectionVOEle.put("paramName", "null");
attrSelectionVOEle.put("paramValue", "null");
attrSelectionVOEle.put("type", 0);
System.out.println(requBody.toJSONString());
}
总结:
方式二的 putArray() 及 putObject() 可以在调用对象上返回一个空的 JSONArray 和 空的JSONObject,让我们不再去new对象及关注何时做映射,比方式一灵活
转JSON字符串时保留null
构造以下JSON串,保留 null:
{
"attrSelectionVO": [
{
"attrAccessId": "eea99a0894504a2b89f3cfeb4be051d3",
"paramName": null,
"paramValue": null
}
],
"categoryCode": "C02"
}
实现:
@Test
public void test1() {
Map<String, Object> map1 = new HashMap<>();
map1.put("categoryCode", "C02");
List<Map<String, Object>> attrSelectionVO = new ArrayList<>();
Map<String, Object> attrSelectionVOEle = new HashMap<>();
attrSelectionVOEle.put("attrAccessId", "eea99a0894504a2b89f3cfeb4be051d3");
attrSelectionVOEle.put("paramName", null);
attrSelectionVOEle.put("paramValue", null);
attrSelectionVO.add(attrSelectionVOEle);
map1.put("attrSelectionVO", attrSelectionVO);
String s = JSON.toJSONString(map1, SerializerFeature.WriteMapNullValue);
System.out.println(s);
}
com.alibaba.fastjson.serializer.SerializerFeature.WriteMapNullValue: fast json 的序列化策略,保留 null 值
https://blog.csdn.net/luoww1/article/details/66973627/
https://blog.csdn.net/weixin_39827470/article/details/109235313
https://blog.csdn.net/weixin_44326073/article/details/128924032
JSON串 转换为 map 及 JSONObject
转map:
@Test
public void test4() {
String s = "{\n" +
" \"attrSelectionVO\": [\n" +
" {\n" +
" \"attrAccessId\": \"eea99a0894504a2b89f3cfeb4be051d3\",\n" +
" \"paramName\": null,\n" +
" \"paramValue\": null\n" +
" }\n" +
" ],\n" +
" \"categoryCode\": \"null\"\n" +
"}";
Map<String, Object> stringObjectMap = JSON.parseObject(s, new TypeReference<Map<String, Object>>() {
});
System.out.println(stringObjectMap); //out: {categoryCode=null, attrSelectionVO=[{"attrAccessId":"eea99a0894504a2b89f3cfeb4be051d3"}]}
System.out.println(JSON.toJSONString(stringObjectMap)); //out:{"categoryCode":"null","attrSelectionVO":[{"attrAccessId":"eea99a0894504a2b89f3cfeb4be051d3"}]}
}
问题:值为 null 的转换为map 后 键不见了
转 JSONObject:
@Test
public void test3() {
String s = "{\n" +
" \"attrSelectionVO\": [\n" +
" {\n" +
" \"attrAccessId\": \"eea99a0894504a2b89f3cfeb4be051d3\",\n" +
" \"paramName\": null,\n" +
" \"paramValue\": null\n" +
" }\n" +
" ],\n" +
" \"categoryCode\": \"null\"\n" +
"}";
JSONObject jsonObject = JSON.parseObject(s);
System.out.println(jsonObject); //out: {"categoryCode":"null","attrSelectionVO":[{"attrAccessId":"eea99a0894504a2b89f3cfeb4be051d3"}]}
System.out.println(JSON.toJSONString(jsonObject,SerializerFeature.WriteMapNullValue)); //out: {"categoryCode":"null","attrSelectionVO":[{"attrAccessId":"eea99a0894504a2b89f3cfeb4be051d3","paramName":null,"paramValue":null}]}
}
转换完毕后 值为null 的键依然存在
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/18115516