Problem overview
About this problem, it is the problem encountered when cooperating with a third-party company to remotely call the service interface (which is equivalent to calling the remote service provider), "java.lang.ClassCastException: java.lang.String cannot be cast to com.alibaba.fastjson.JSONObject" means that the JSON string cannot be converted into a JSONObject object.
Some abnormal screenshots are as follows:
Specific information is as follows
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.alibaba.fastjson.JSONObject at com.alibaba.fastjson.JSON.parseObject(JSON.java:229) ... 56 more
Solution
After receiving the information, the calling interface uses alibaba's fastjason tool class to deserialize. The example code is as follows:
@Test public void fastjson() { // Serialize the employee object as a Json string String jsonString = JSON.toJSONString(initEmployee()); System.out.println(jsonString); // Deserialize Json strings into Java objects Employee employee = JSON.parseObject(jsonString, Employee.class); System.out.println("empName: " + employee.getName()); }
Or Jackson (fastjason is better in supporting generics than fastjason). The example code is as follows:
@Test public void jackson() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); // Serialize Java objects into Json strings String asString = objectMapper.writeValueAsString(initEmployee()); System.out.println(asString); // Deserialize Json strings into Java objects Employee employee = objectMapper.readValue(asString, Employee.class); System.out.println("empName: " + employee.getName()); }
Of course, you can also use Google's Gson. The example code is as follows:
@Test public void gson() { Gson gson = new GsonBuilder().create(); // Serialize Java objects into Json strings String json = gson.toJson(initEmployee()); System.out.println(json); // Deserialize Json strings into Java objects Employee employee = gson.fromJson(json, Employee.class); System.out.println("empName: " + employee.getName()); }
Troubleshooting:
Looking at the result returned by the call, I found a surprising scene. There are many escape characters in the returned call result, which is not a correct JSON string. Of course, it cannot be serialized successfully. The example content is as follows
"{\"id\":\"1001\",\"name\":\"Zhang San\",\"password\":\"zhangsan\"}"
(of course, the above is only to prove the test data of the problem. The actual data interface data is much more complex than this, and a special encryption algorithm is carried out, but the returned data format is the same as above, and the final solution is the same)
terms of settlement:
1. By manually eliminating escape characters, the example code is as follows:
@Test public void tData() { String data01 = "{\"id\":\"1001\",\"name\":\"Zhang San\",\"password\":\"zhangsan\"}"; System.out.println("data01: " + data01); // Serialize to JSON string String data02 = JSON.toJSONString(data01); System.out.println("data02: " + data02); //Remove slashes with replace String data03 = data02.replace("\\", ""); System.out.println("data03: " + data03); //Remove the quotation marks at the beginning and end String data04 = data03.substring(1, data03.length() - 1); System.out.println("data04: " + data04); Map map = JSON.parseObject(data04, Map.class); System.out.println(map.get("name")); }
The desired results are as follows:
However, there is a problem with the above. Each conversion will traverse all the contents. When the length of the content is small, it doesn't matter, but when the length is long, the problem will come and consume a lot of time. For example, the blogger has a large amount of data, and it will take one to two minutes to process the results without calling the interface. This...
Performance Optimization: there are many string processing tool classes in Apache's commons Lang toolkit, among which StringEscapeUtils is one. However, it should be noted that StringEscapeUtils is added only after version 2.3 or above. However, bloggers began to abandon it after seeing 3.0 on the official website and moved the function to the Commons text toolkit, so you need to pay attention to importing the corresponding package!!!
If commons Lang is used, add dependencies:
<dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency>
Add dependencies if commons text is used:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.6</version> </dependency>
Use the StringEscapeUtils tool class to deserialize escape characters. The example is as follows:
@Test public void tData() { String data01 = "{\"id\":\"1001\",\"name\":\"Zhang San\",\"password\":\"zhangsan\"}"; System.out.println("data01: " + data01); // Serialize to JSON string String data02 = JSON.toJSONString(data01); System.out.println("data02: " + data02); // Deserialize escape characters through the StringEscapeUtils utility class String data04 = StringEscapeUtils.unescapeJava(data01); System.out.println("data04: " + data04); Map map = JSON.parseObject(data04, Map.class); System.out.println(map.get("name")); }
The results are the same when used, but the effect of using stringescape utils is better when dealing with large and long string contents!!!
Of course, StringEscapeUtils also has many fun and very useful methods, which need to be played by yourself, as shown in the following figure: