Home  >  Article  >  Web Front-end  >  How to automatically convert upper and lower case when jackson parses json string

How to automatically convert upper and lower case when jackson parses json string

php中世界最好的语言
php中世界最好的语言Original
2018-03-15 14:30:393881browse

This time I will bring you the method of automatic case conversion when jackson parses a json string. What are the things to pay attention to when jackson parses a json string? The following is a practical case, let's take a look one time.

Problem

The problem the poster encountered is that a certain field defined in the entity class and table is RMBPrice, with the first letter capitalized. The column name from sql

query is also RMBPrice in uppercase, but when using datatables of jquery to initialize the column, an error occurs.

The code for that line is as follows:

{"name": "RMBPrice", "data": "RMBPrice", "className": "text-center", "render": formatRMBPrice},

This will be displayed when opening the page. Check the value returned by the browser calling interface:

is lowercase rmbprice

Analysis

The interface returns a @ResponseBody object. From the break point in the code, the return value of the interface is all in uppercase, so it can only be conversion. There was a problem when getting to json.

If the first letter of Baidu json is capitalized, articles will be automatically converted to lowercase.

After testing, it is true that the current few letters that are all uppercase will be converted to lowercase until they are no longer uppercase. If there is an uppercase after the lowercase, it will remain in uppercase.

For example: RRRddRRR will become rrrddRRR.

Solution

I directly modify the line of code in js and change After querying:

{"name": "rmbprice", "data": "rmbprice", "className": "text-center", "render": formatRMBPrice},
, if you want to retain uppercase, you need to add annotations.

For example, jackson uses:

When defining fields in entity classes:

@JsonProperty("ActionCode") 
 private String ActionCode = "";

fastjson uses:

@JSONField(name = “Name”) And this tag should be marked before the get method:

public class User { 
 private String name; 
 private int age; 
 @JSONField(name = "Name") 
 public String getName(){
  return name;
 } 
 public void setName(String name){
  this.name = name;
 }
 @JSONField(name = "Age") 
 public int getAge(){
  return age;
 } 
 public void setAge(int age){
  this.age= age;
 }
}

I believe you have mastered the method after reading the case in this article. For more exciting content, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to deal with no reflection after successful ajax request for background data

jQuery EasyUI tab panel tabs use

The above is the detailed content of How to automatically convert upper and lower case when jackson parses json string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn