search

Home  >  Q&A  >  body text

android - How to ensure that "\r" is not cleared when Gson parses characters containing "\r"?

    String json = "{\"remark1\":\"这里是一个反斜杠 + r \r \r \r ------------- \",\"remark2\":\"这里是二个反斜杠 + r \\r \\r \\r ------------- \",\"remark3\":\"这里是三个反斜杠 + r \\\r \\\r \\\r ------------- \"}";

I have a Json string that I want to display and wrap in TextView, but during the actual operation, I use Gson to convert it into Bean## After the #object, "\r" will always be removed directly by Gson.

I tried

disableHtmlEscaping()It didn’t work!!!

PLog.json(json);

Gson gson = new GsonBuilder().disableHtmlEscaping().create();
Gson gson2 = new Gson();

ReMark shareLinkResponse = gson.fromJson(json, ReMark.class);
ReMark shareLinkResponse2 = gson2.fromJson(json, ReMark.class);
PLog.d(shareLinkResponse.getRemark1());
PLog.d(shareLinkResponse.getRemark2());
PLog.d(shareLinkResponse.getRemark3());
PLog.d("----------------------------------------");
PLog.d(shareLinkResponse2.getRemark1());
PLog.d(shareLinkResponse2.getRemark2());
PLog.d(shareLinkResponse2.getRemark3());

Show

in my LogCat

 {
     "remark1": "这里是一个反斜杠 + r \r \r \r ------------- ",
     "remark2": "这里是二个反斜杠 + r \r \r \r ------------- ",
     "remark3": "这里是三个反斜杠 + r \\r \\r \\r ------------- "
 }

D/PLog: [ (MainActivity.java:49)#onCreate ] 这里是一个反斜杠 + r    ------------- 
D/PLog: [ (MainActivity.java:50)#onCreate ] 这里是二个反斜杠 + r \r \r \r ------------
D/PLog: [ (MainActivity.java:51)#onCreate ] 这里是三个反斜杠 + r \ \ \ ------------- 
D/PLog: [ (MainActivity.java:52)#onCreate ] ----------------------------------
D/PLog: [ (MainActivity.java:53)#onCreate ] 这里是一个反斜杠 + r    ------------- 
D/PLog: [ (MainActivity.java:54)#onCreate ] 这里是二个反斜杠 + r \r \r \r ------------
D/PLog: [ (MainActivity.java:55)#onCreate ] 这里是三个反斜杠 + r \ \ \ ------------- 

If

Gson cannot be parsed correctly, I will have to do it manually, which is very embarrassing

HELP

女神的闺蜜爱上我女神的闺蜜爱上我2724 days ago1457

reply all(1)I'll reply

  • 黄舟

    黄舟2017-07-04 13:47:28

    Actually

    这里是一个反斜杠 + r    -------------

    Herer exists, but the logcat of Android Studio does not display it, and the line break under win is not r, so it is not discovered immediately. If you take this string and go to contains ("r") What is returned at this time is true, which proves that there is r in the string, haha

    reply
    0
  • Cancelreply