Home > Back-end >  FastJson json string into JSONObject, how to retain the value to null field
FastJson json string into JSONObject, how to retain the value to null field

Time:09-27

As title, FastJson USES are not ripe, the following is a piece of code, resultStr is a json string, which contain null fields, converted to is not the null field after JSONObject, how can I keep?
JSONObject JSONObject=JSON. ParseObject (resultStr);
I this is not converted to encapsulate good class object, because resultStr structure is uncertain, so are USES JSONObject to operation!
How to put it bluntly, is to convert the JSONObject retains the null fields, beg god for help to see see!

CodePudding user response:

ParseObject) allowing one preach a ParseProcess interface implementation as parameters, the interface has a subinterface call ExtraProcessor, it defines a method called processExtra, there are three parameters is object, the key and the value...
The rest you know

CodePudding user response:

Also hope to express! I also encountered this problem! Only know fastjson weex framework, the processExtra. How to solve the JSON parse (strJson); The method to filter out the null field?

CodePudding user response:

1/f, don't meet the requirements of the building Lord I also encountered this problem, the building Lord solved excuse me

CodePudding user response:

I also encountered this problem, the building Lord solved excuse me

CodePudding user response:

JSONObject. ToJSONString (jsonMap, SerializerFeature WriteMapNullValue);

CodePudding user response:

Map The map=JSONObject. ParseObject (respJson, new TypeReference () {
});
This can be translated into a null value map, so you don't have to consider the key value is null has been erased question (too pit!)

CodePudding user response:

The sixth floor of the solution, for only a layer of json is useful, but if have not, such as there is a json string:
{" k1 ": 1," k2 ": 2," k3 ", null, "k4" : {" k41: "null," k42 ": 42}," k5 ": 5}
Converted by the above map result:
{k1=1, k2=2, k3=null, k4={" k42 ": 42}, k5=5}
The first layer of k3 will retain, k41 was wiped out in the second floor,

CodePudding user response:

refer to 7th floor yjclsx response:
6th floor solution, for only a layer of json is useful, but if have not, such as there is a json string:
{" k1 ": 1," k2 ": 2," k3 ", null, "k4" : {" k41: "null," k42 ": 42}," k5 ": 5}
Converted by the above map result:
{k1=1, k2=2, k3=null, k4={" k42 ": 42}, k5=5}
K3 will be kept in the first layer, the k41 was wiped out in the second,
you can layer it out each layer USES the method resolution

CodePudding user response:

If a single analysis please refer to the fifth floor, if the project need to parse null, requires the project startup initialization fastjson Provider, add SerializerFeature strategy for WriteMapNullValue

CodePudding user response:

Factor way upstairs is not very good, I need to parse the first layer, get the value value is on the second floor parsing, and, for the first time resolution NULL value properties are filtered out, at the back of the parse it doesn't make any sense,,,
The approach is on my side: the field list of null is replaced with a special character, so he is not null values, can all parsing,
Violence method is simple, but it also can solve the problem, wait for a better way to accomplish this,

CodePudding user response:

Judgment, null assignment into ""

CodePudding user response:

Judgment, null assignment into ""

CodePudding user response:

Cn. Hutool. Json. JSONObject analytical
use this to do
String String=jedisPoolUtil. Get (STR);
JSONObject Object=JSONUtil. ParseObj (string);
Object. The remove (" clientIP ");
Return the Result. The return (200, 1, JSONUtil. ToJsonStr (Object));

Result in class so that you can

Public static String Return (int code, int total, String RST) {
String STR="{" CODE \ ":" + CODE + ", \ "TOTAL " : "+ TOTAL +", \ "RST " : "+ RST +"} ";
return str;
}
So at the front desk can get Json string!

CodePudding user response:

Reference:

Fastjson is converted to a json string, retains the null field

CodePudding user response:

Using com. Alibaba. Druid. Support. Json. JSONUtils. ToJSONString (Object), the first Object into a string, loaded is keep null values, then think how to do how to do

CodePudding user response:

In json conversion, most are carried out in accordance with the default serialization way, if you need special processing, can be used in the transformation SerializerFeature, for example: resultJson. ToJSONString (SerializerFeature. WriteMapNullValue)
  • Related