简体   繁体   English

从json响应中删除unicode字符串和空格

[英]Remove unicode string and spaces from json response

I am calling a webservice which gives me Json Response. 我正在调用一个Web服务,它给了我Json Response。 Here is the code 这是代码

URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
...
//get response text
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));           
StringBuffer response = new StringBuffer();
while ((respText = br.readLine()) != null) {                
    response.append(respText);
}
br.close(); 
retValue = response.toString();

The problem is response is like the following 问题是响应如下

"{\u000d\u000a  \"groups\": [\u000d\u000a    {\u000d\u000a      \"signingGroupId\": \"323793\",\u000d\u000a      \"groupName\": \"1440 Signing Group 1\",\u000d\u000a      \"groupType\": \"sharedSigningGroup\",\u000d\u000a      \"created\": \"3\/27\/2019 6:08:34 AM\",\u000d\u000a      \"createdBy\": \"ABC Software\",\u000d\u000a      \"modified\": \"4\/1\/2019 6:40:45 AM\",\u000d\u000a      \"modifiedBy\": \"Corner O'Brien\"\u000d\u000a    },\u000d\u000a    {\u000d\u000a      \"signingGroupId\": \"323794\",\u000d\u000a      \"groupName\": \"1440 Another Group\",\u000d\u000a      \"groupType\": \"sharedSigningGroup\",\u000d\u000a      \"created\": \"3\/27\/2019 6:14:52 AM\",\u000d\u000a      \"createdBy\": \"XYZ Software\",\u000d\u000a      \"modified\": \"3\/27\/2019 6:16:37 AM\",\u000d\u000a      \"modifiedBy\": \"XYZ Software\"\u000d\u000a    }\u000d\u000a  ]\u000d\u000a}"

I want to make respose like this from the above response 我想从上面的回复中做出这样的反应

"{"groups":[{"signingGroupId": "323793","groupName": "1440 Signing Group 1","groupType": "sharedSigningGroup","created": "3/27/2019 6:08:34 AM","createdBy": "ABC Software","modified": "4/1/2019 6:40:45 AM","modifiedBy": "John O'Brien"},{"signingGroupId": "323794","groupName": "1440 Another Group","groupType": "sharedSigningGroup","created": "3/27/2019 6:14:52 AM","createdBy": "ABC Software","modified": "3/27/2019 6:16:37 AM","modifiedBy": "ABC Software"}]}"

That is to remove all the \ \ and spaces after that. 那就是删除之后的所有\ \ spaces But not to remove space from any of the Json Property. 但是不要从任何Json Property中删除空间。

I tried this 我试过这个

retValue = retValue.replace("\\u000d\\u000a", "");
retValue = retValue.replace("\\", "");

which gives me 这给了我

"{  "groups": [    {      "signingGroupId": "323793",      "groupName": "1440 Signing Group 1",      "groupType": "sharedSigningGroup",      "created": "3/27/2019 6:08:34 AM",      "createdBy": "ABC Software",      "modified": "4/1/2019 6:40:45 AM",      "modifiedBy": "John O'Brien"    },    {      "signingGroupId": "323794",      "groupName": "1440 Another Group",      "groupType": "sharedSigningGroup",      "created": "3/27/2019 6:14:52 AM",      "createdBy": "XYZ Software",      "modified": "3/27/2019 6:16:37 AM",      "modifiedBy": "XYZ Software"    }  ]}"

But still there are spaces. 但仍有空间。 Now If I use 现在如果我使用

String replcaedNewLineAndSpaces = retValue.replaceAll("[ \t]+", "");

Problem is with this line is it also removes spaces from the Json Properties. 问题在于这条线是否也会从Json Properties中删除空格。

"{"groups":[{"signingGroupId":"323793","groupName":"1440SigningGroup1","groupType":"sharedSigningGroup","created":"3/27/20196:08:34AM","createdBy":"ABCSoftware","modified":"4/1/20196:40:45AM","modifiedBy":"JohnO'Brien"},{"signingGroupId":"323794","groupName":"1440AnotherGroup","groupType":"sharedSigningGroup","created":"3/27/20196:14:52AM","createdBy":"XYZSoftware","modified":"3/27/20196:16:37AM","modifiedBy":"XYZSoftware"}]}"

See groupName which was "groupName": "1440 Signing Group 1" . 请参阅groupName,即"groupName": "1440 Signing Group 1" Now becomes 1440SigningGroup1 . 现在变成1440SigningGroup1 Similarly for other properties. 其他属性也是如此。

Is there anyway It keeps the Json String as it is but remove \ \ and spaces after it. 无论如何它保持Json字符串\ \ 但删除\ \ spaces So the final response become 所以最后的回应变成了

"{"groups":[{"signingGroupId": "323793","groupName": "1440 Signing Group 1","groupType": "sharedSigningGroup","created": "3/27/2019 6:08:34 AM","createdBy": "ABC Software","modified": "4/1/2019 6:40:45 AM","modifiedBy": "John O'Brien"},{"signingGroupId": "323794","groupName": "1440 Another Group","groupType": "sharedSigningGroup","created": "3/27/2019 6:14:52 AM","createdBy": "ABC Software","modified": "3/27/2019 6:16:37 AM","modifiedBy": "ABC Software"}]}"

Thanks 谢谢

As referred in the comments, creating a POJO class and parsing the JSON would be optimal and safe. 正如评论中所提到的,创建POJO类并解析JSON将是最佳和安全的。 (I think the most common library is GSon library ) (我认为最常见的库是GSon库

However, if you insist removing spaces though, do it with a simple regex: 但是,如果您坚持删除空格,请使用简单的正则表达式:

retValue.replaceAll("\\\\u000d\\\\u000a\\s*", "")

This will replace all strings starting with \ \ followed by 0-unlimited spaces. 这将替换以\\ u000d \\ u000a开头的所有字符串,后跟0-无限制空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM