Json format error is generally caused by a problem between the json data on the server side and the parsing format of your client.
Solution:
1, Use org.json.JSONObject to parse from When getting json data from the network, I encounter JSONException: java.lang.String cannot be converted to JSONObject. There is no problem when printing the string and checking the json string;
2, The reason for the problem is that when writing the json file, utf-8 encoding is used. utf8 has a BOM format, and it will be ok if you remove this format;
3 , Another way is to remove the BOM header in the java code.
The Select method reprojects the object collection and converts the Birthday attribute into a new attribute. Note that the attribute must be renamed after it is changed. The attribute name can be the same; you can use the select method or LINQ query expression here. , you can also choose other methods to achieve the same purpose; this method can remove attributes that are not used by the client in the collection to achieve the purpose of simply optimizing performance.
The backend provides APIs for front-end calls. Generally, HttpMessageConverter is used to convert Json strings into objects. If there is a format problem, an exception HttpMessageNotReadableException will be thrown. However, the original Json string cannot be obtained in the background, and it is inconvenient to locate the exact format error. This article introduces a simple method to display the original Json information when an error occurs.
At first I wanted to find other ways. When an exception is thrown, read the body content from HttpServletRequest. But it can't be done because when reading Json, the InputStream has been read and the body content cannot be obtained again. So you can customize MessageConverter.
The above is the detailed content of What does json format error mean?. For more information, please follow other related articles on the PHP Chinese website!