Recently, I encountered A problem in development: “Server B cannot parse the JSON string sent by server A.”

The string sent from the front end to the back end is more complex, and there are many escape characters “\” and other information in the middle. After getting the string, the background needs to add part of the content and then assemble it into JSON string and send it to another system for processing.

The assembled JSON string looks like this:

"Username ": "test", "tranType": "00", "authType": ["00"], "devices":{ "deviceID": "+XYZabcfefafaf\/efffahfsahfQ" } } "Response":"[{\"asseration\":[{\"asseration\":\"effafhahfahfahfsa\",\"name\":\"Xiaoming\"}],\"pv\":{\"major\":1}}]" }Copy the code

Primary mode:Using string concatenation

String username = "test";
String tranType = "00";
String authType = "[\"00\"]";
String deviceID = inVo.getDeviceID();
String Response = inVo.getResponse();
String request = "{\"Response\":\"" + Response + 
              "\", \"context\":{\"username\":\"" + username +
              "\", \"tranType\":\"" + tranType +
              "\", \"authType\":\"" + authType +
              "  , \"devices\"{\"devideID\":\"" + deviceID + "\"}}}"
Copy the code

The actual test shows that when the assembled JSON string is relatively simple (excluding deviceID and Response), server B can correctly parse and process the JSON string.

However, if the assembled JSON string contains deviceID and Response, the final assembled request will identify “\” as an escape character. As a result, some information is missing in the JSON sent to the background and cannot be parsed.

Advanced way:Use Map for assembly

String username = "test";
String tranType = "00";
String authType = "[\"00\"]";
String deviceID = inVo.getDeviceID();
String Response = inVo.getResponse();
Map<String, Object> request = new HashMap()<>;
Map<String, Object> contextInfo = new HashMap()<>;
Map<String, Object> deviceInfo = new HashMap()<>;
deviceInfo.put("devideID", deviceID);
contextInfo.put("username", username);
contextInfo.put("tranType", tranType);
contextInfo.put("authType", authType);
contextInfo.put("devices", deviceInfo);
request.put("context", contextInfo);
request.put("Response", Response);
Copy the code

[“00”] will be processed as a whole (string “[“00″]”). As a result, some information is added to the JSON sent to the background and cannot be parsed.

Final method:Use JSONObject for assembly

String username = "test";
String tranType = "00";
JSONObject request = new JSONObject();
JSONObject contextInfo = new JSONObject();
JSONObject deviceInfo = new JSONObject();
deviceInfo.put("devideID", inVo.getDeviceID());
contextInfo.put("username", username);
contextInfo.put("tranType", tranType);
    JSONArray authType = new JSONArray();
    authType.put("00");   // "authType": ["00"],
    contextInfo.put("authType", authType);
contextInfo.put("devices", deviceInfo);
request.put("context", contextInfo);
request.put("Response", inVo.getResponse());
Copy the code

Without further ado, the test was successful!

At the same time, a JSONObject can convert to and from a String

request.toString()
JSONObject object = new JSONObject(request.toString());
Copy the code