GSON issue with String

前端 未结 2 1132
有刺的猬
有刺的猬 2020-12-23 19:45
    String s = \"m\\\\\"+\"/m\\\\/m/m/m/m/m\";

    LinkedHashMap hm = new LinkedHashMap<>();

    hm.put(\"test\", s);

    System.out.p         


        
相关标签:
2条回答
  • 2020-12-23 20:34

    Since some people like to nitpick, I'll add the answer to the question (even though it was already answered and chosen as the correct answer) ...

    I agree with the chose answer to this question, use the following code:

    Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    String s2 = gson.toJson(hm.toString());
    s2.replace("\\\\", "\\");
    

    @Bajrang Hudda has asked about \n ... I hit this issue recently ... I was able to solve it using:

    Gson gson = new Gson();
    String json = (gson.toJson(data)).replaceAll("\\\\n", "\\n");
    
    0 讨论(0)
  • 2020-12-23 20:38

    The = sign is encoded to \u003d. Hence you need to use disableHtmlEscaping().

    You can use

    Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    String s2 = gson.toJson(hm.toString());
    

    For \/ turning into \\/ issue, the solution is

    s2.replace("\\\\", "\\");
    
    0 讨论(0)
提交回复
热议问题