Home  >  Article  >  Web Front-end  >  How to operate the case conversion of the first letter when Jackson parses a json string

How to operate the case conversion of the first letter when Jackson parses a json string

php中世界最好的语言
php中世界最好的语言Original
2018-04-14 09:21:493549browse

This time I will show you how to convert the case of the first letter when Jackson parses a json string, and how to convert the case of the first letter when Jackson parses a json stringNote What are the matters? Below are practical cases. Let’s take a look.

question

The problem encountered by the poster is that a certain field defined in the entity class and table is RMBPrice, with the first letter in capital letters. The column name returned by sqlquery is also RMBPrice in capital letters, but using jqueryWhen datatables initialized columns, an error occurred.

That line of code is as follows:

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

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

is lowercase rmbprice

analyze

The interface returns a @ResponseBody object. From the break point in the code, the return value of the interface is all in uppercase, so there may only be a problem when converting 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 are uppercase letters after the lowercase letters, they will remain uppercase.

For example: RRRddRRR will become rrrddRRR.

Solution

I directly modified the line of code in js to:

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

After querying, if you want to retain capitalization, 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 the 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;
 }
}

also said:

Force conversion, use com.alibaba.fastjson.serializer.PascalNameFilter to directly convert the first letter to uppercase. For example: JSON.toJSONString(bean,new PascalNameFilter());

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

Recommended reading:

How to merge JS Object values

Angular operates the table to sort it

The above is the detailed content of How to operate the case conversion of the first letter when Jackson parses a 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