我正在呼叫一个Web服务,该服务给了我Json Response。这是代码
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();
问题是响应如下所示
"{\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}"
我想从上述回复中做出这样的休养
"{"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"}]}"
那是在那之后删除所有的\u000d\u000a
和spaces
。但不要从任何Json属性中删除空间。
我尝试过
retValue = retValue.replace("\\u000d\\u000a", "");
retValue = retValue.replace("\\", "");
这给了我
"{ "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" } ]}"
但是仍然有空格。现在,如果我使用
String replcaedNewLineAndSpaces = retValue.replaceAll("[ \t]+", "");
此行存在问题,因为它还会从“ Json属性”中删除空格。
"{"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"}]}"
请参阅groupName,它是"groupName": "1440 Signing Group 1"
。现在变成
1440SigningGroup1
。其他属性也是如此。
仍然存在吗?它将Json String保持原样,但在其后删除\u000d\u000a
和spaces
。因此,最终的响应变为
"{"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"}]}"
谢谢
答案 0 :(得分:0)
如评论中所述,创建一个POJO类并解析JSON是最佳且安全的。 (我认为最常见的库是GSon library)
但是,如果您坚持要删除空格,请使用简单的正则表达式进行操作:
retValue.replaceAll("\\\\u000d\\\\u000a\\s*", "")
这将替换所有以\ u000d \ u000a开头的字符串,后跟0个无限制的空格。