使用Jackson库创建带有反斜杠和正斜杠的JSON。

huangapple go评论78阅读模式
英文:

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 = &quot;1022&quot;; //I can format it as needed
Using Jackson libray I need to create the following JSON:

{
"cardExpiration":"10/22"
}


I&#39;m using the following code
    String cardExpiration = &quot;10\\/22&quot;;
    MyPojo myPojo = new MyPojo(cardExpiration);
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(myPojo);
This code returns JSON with &quot;10\\\\/22&quot; (double backslashes, but I need just one backslash).
I can not create in Java String &quot;10\\/22&quot;, as it is illegal escape character in string literal.
Any idea how to get in JSON **&quot;10\\/22&quot;**?


</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 = &quot;10\\/22&quot;;
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 {&quot;cardExpiration&quot;:10\/22} you can add to the attribute in the POJO the @JsonRawValue annotation.

<b>pom.xml</b><br>

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;com.mycompany.app&lt;/groupId&gt;
    &lt;artifactId&gt;my-app&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;properties&gt;
        &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
        &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
    &lt;/properties&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
            &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
            &lt;version&gt;2.11.2&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;

<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 = &quot;10\\/22&quot;;
        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 {&quot;cardExpiration&quot;:&quot;10\\/22&quot;}
    }
    
}

class MyPojo {
    String cardExpiration;

    public MyPojo(String cardExpiration) {
        this.cardExpiration = cardExpiration;
    }

    public String getCardExpiration() {
        return cardExpiration;
    }

    public void setCardExpiration(String cardExpiration) {
        this.cardExpiration = cardExpiration;
    }
}

huangapple
  • 本文由 发表于 2020年8月21日 23:07:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63525401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定