英文:
Create JSON backslash + slash with Jackson library
问题
在Java类中有一个字段
String cardExpiration = "1022"; //根据需要进行格式化
使用Jackson库,我需要创建以下JSON:
{
"cardExpiration":"10\/22"
}
我正在使用以下代码
String cardExpiration = "10\\/22";
MyPojo myPojo = new MyPojo(cardExpiration);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(myPojo);
这段代码返回的JSON是"10\\/22"(双斜杠),但我只需要一个反斜杠。
我无法在Java中创建字符串"10\/22",因为在字符串字面量中它是非法的转义字符。
有什么办法可以在JSON中获得 "10\/22" 吗?
<details>
<summary>英文:</summary>
In Java class there is a field
String cardExpiration = "1022"; //I can format it as needed
Using Jackson libray I need to create the following JSON:
{
"cardExpiration":"10/22"
}
I'm using the following code
String cardExpiration = "10\\/22";
MyPojo myPojo = new MyPojo(cardExpiration);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(myPojo);
This code returns JSON with "10\\\\/22" (double backslashes, but I need just one backslash).
I can not create in Java String "10\\/22", as it is illegal escape character in string literal.
Any idea how to get in JSON **"10\\/22"**?
</details>
# 答案1
**得分**: 0
Escape the backslash with a second one to define it as String.
```java
String cardExpiration = "10\\/22";
System.out.println(cardExpiration); // prints 10\\/22
Here a working example that includes the usage of Jackson for serializing your POJO. Note that Jackson escapes the Backslash inside the JSON auto. for you. If you really want to have the JSON value {\"cardExpiration\":10\\/22}
you can add to the attribute in the POJO the @JsonRawValue
annotation.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
</dependencies>
</project>
App.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class App {
public static void main(String[] args) throws JsonProcessingException {
String cardExpiration = "10\\/22";
System.out.println(cardExpiration); // prints 10\\/22
MyPojo myPojo = new MyPojo(cardExpiration);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(myPojo);
System.out.println(json); // prints {"cardExpiration":"10\\/22"}
}
}
class MyPojo {
String cardExpiration;
public MyPojo(String cardExpiration) {
this.cardExpiration = cardExpiration;
}
public String getCardExpiration() {
return cardExpiration;
}
public void setCardExpiration(String cardExpiration) {
this.cardExpiration = cardExpiration;
}
}
英文:
Escape the backslash with a second one to define it as String.
String cardExpiration = "10\\/22";
System.out.println(cardExpiration); // prints 10\/22
Here a working example that includes the usage of Jackson for serializing your POJO. Note that Jackson escapes the Backslash inside the JSON auto. for you. If you really want to have the JSON value {"cardExpiration":10\/22}
you can add to the attribute in the POJO the @JsonRawValue annotation.
<b>pom.xml</b><br>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
</dependencies>
</project>
<b>App.java<b>
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class App {
public static void main(String[] args) throws JsonProcessingException {
String cardExpiration = "10\\/22";
System.out.println(cardExpiration); // prints 10\/22
MyPojo myPojo = new MyPojo(cardExpiration);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(myPojo);
System.out.println(json); // prints {"cardExpiration":"10\\/22"}
}
}
class MyPojo {
String cardExpiration;
public MyPojo(String cardExpiration) {
this.cardExpiration = cardExpiration;
}
public String getCardExpiration() {
return cardExpiration;
}
public void setCardExpiration(String cardExpiration) {
this.cardExpiration = cardExpiration;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论