Resolving string string to object JSON format error in Fasterxml

Source: Internet
Author: User

The package that Jackson used in Springboot was fasterxml. You can convert a string in JSON format to a Java object by using the following code:

New Com.fasterxml.jackson.databind.ObjectMapper (); Mapper.readvalue (String, JavaBean. class);

However, when we want to turn the string is this format will be an error, because this format is not the canonical JSON format:

{success:2,message: ' Certification has passed! The device is not added, please contact the airline. Device code: 1101 '}

The error message is:

com.fasterxml.jackson.core.JsonParseException:Unexpected character (' s ' (Code)): Was expecting Double-quote to Start field name

Fasterxml expects field names to be enclosed in double quotes, which is expected:

{"Success": 2, "message": "Authentication passed!" The device is not added, please contact the airline. Device code: 1101 "}

By contrast, it was found that the above string and the canonical JSON string have two different points, one is the field name is not quoted, the second is a single quotation mark. These are not standard JSON format notation.

The best solution, of course, is to turn string strings into the canonical JSON format, but for some reason, you have to use this format.

Solve:

New Com.fasterxml.jackson.databind.ObjectMapper (); // allows the use of unquoted field names mapper.configure (feature.allow_unquoted_field_namestrue); // single quote mapper.configure (feature.allow_single_quotestrue) is allowed; Mapper.readvalue ( String, JavaBean. class);

In addition to the allow_unquoted_field_names,allow_single_quotes there are other settings, useful to try.

Resolving string string to object JSON format error in Fasterxml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.