从多部分HTTP响应中提取多个JSON字符串

3

我正在使用Apache HttpClient来处理返回multipart/form-data响应的Web服务,并且该响应包含JSON。

我非常难以将每个JSON字符串分离出来,以便读取JSON字符串。

我阅读了Stackoverflow上类似的帖子,有些人建议使用Apache commons fileupload,但我不确定它如何从整个响应中分离出JSON字符串,因为响应中还有许多其他文本,例如边界字符串、内容类型等。

响应的格式大致如下。

--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetMailboxes
Status-Code: 200
X-Server-Response-Time: 4ms
X-Server-Chain: domain.com
Content-RequestDuration: 5

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetFolders
Status-Code: 200
X-Server-Response-Time: 8ms
X-Server-Chain: domain.com
Content-RequestDuration: 10

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetAlerts
Status-Code: 200
X-Server-Response-Time: 10ms
X-Server-Chain: domain.com
Content-RequestDuration: 12

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetAccounts
Status-Code: 200
X-Server-Response-Time: 11ms
X-Server-Chain: domain.com
Content-RequestDuration: 12

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetAllSavedSearches
Status-Code: 200
X-Server-Response-Time: 10ms
X-Server-Chain: domain.com
Content-RequestDuration: 12

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetAthenaSegment
Status-Code: 200
X-Server-Response-Time: 14ms
Content-RequestDuration: 21

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: ListFolderThreads
Status-Code: 200
X-Server-Response-Time: 110ms
Content-RequestDuration: 116

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-Type: application/json
Content-RequestId: GetUserInfo
Status-Code: 200
X-Server-Response-Time: 197ms
Content-RequestDuration: 204

{JSON}
--Boundary_16003419_2104021487_1483424496169
Content-RequestId: Status
Content-Type: application/json

{JSON}
--Boundary_16003419_2104021487_1483424496169--

有可靠的方法来完成这个吗?

你的请求是什么样子的?你请求的是 application/json 吗?这个返回的起始部分完整吗?看起来不对,Content-Type 应该包含 boundary 标签。你如何使用 httpclient 读取响应数据? - PeterMmm
@PeterMmm 我正在请求 "application/json; charset=utf-8"。我已经编辑了问题并发布了完整的响应。这就是它看起来像100%与JSON内容更改为{JSON}。 - Arya
响应以 --Boundary_16003419... 开头?这是什么服务器?你控制它吗? - PeterMmm
@PeterMmm 很不幸,我无法控制它。但是我必须得和它一起工作 :( - Arya
我最终使用了以下正则表达式(\{.*?\})——看起来运行良好。 - Arya
1个回答

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接