以下是批量请求响应示例。
HTTP/1.1 200 OK
Content-Type: multipart/mixed; boundary=batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Date: Tue, 22 Jan 2013 18:56:00 GMT
Expires: Tue, 22 Jan 2013 18:56:00 GMT
Cache-Control: private, max-age=0
--batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Content-Type: application/http
Content-ID: response-TIMELINE_INSERT_USER_1
HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 304
{
"kind": "glass#timelineItem",
"id": "1234567890",
"selfLink": "https://www.googleapis.com/mirror/v1/timeline/1234567890",
"created": "2012-09-25T23:28:43.192Z",
"updated": "2012-09-25T23:28:43.192Z",
"etag": "\"G5BI0RWvj-0jWdBrdWrPZV7xPKw/t25selcGS3uDEVT6FB09hAG-QQ\"",
"text": "Hello there!"
}
--batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Content-Type: application/http
Content-ID: response-TIMELINE_INSERT_USER_2
HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 304
{
"kind": "glass#timelineItem",
"id": "0987654321",
"selfLink": "https://www.googleapis.com/mirror/v1/timeline/0987654321",
"created": "2012-09-25T23:28:43.192Z",
"updated": "2012-09-25T23:28:43.192Z",
"etag": "\"G5BI0RWvj-0jWdBrdWrPZV7xPKw/t25selcGS3uDEVT6FB09hAG-QQ\"",
"text": "Hello there!"
}
--batch_pK7JBAk73-E=_AA5eFwv4m2Q=
Content-Type: application/http
Content-ID: response-TIMELINE_INSERT_USER_3
HTTP/1.1 201 Created
Content-Type: application/json
Content-Length: 304
{
"kind": "glass#timelineItem",
"id": "5432109876",
"selfLink": "https://www.googleapis.com/mirror/v1/timeline/5432109876",
"created": "2012-09-25T23:28:43.192Z",
"updated": "2012-09-25T23:28:43.192Z",
"etag": "\"G5BI0RWvj-0jWdBrdWrPZV7xPKw/t25selcGS3uDEVT6FB09hAG-QQ\"",
"text": "Hello there!"
}
--batch_pK7JBAk73-E=_AA5eFwv4m2Q=--
我的批量请求有类似的回复,但我在收到每个批次kind
时遇到问题。如何检索每个批次' kind
??
修改
我使用Googl的批量请求获得类似的回复。
gclient = Google::APIClient.new(....)
batch = Google::APIClient::BatchRequest.new
batch.add(list_salesman_one).add(list_salesman_two)
batch = gclient.execute(batch).to_json
batch_decoded = ActiveSupport::JSON.decode(batch) OR batch_decoded = JSON.parse(batch)
batch_decoded_body = batch_decoded["response"]["body"]
batch_decoded_body
给出了上述回复。变量OR
中的batch_decoded
表示我可以使用其中任何一个。
答案 0 :(得分:1)
您需要分别处理批次中的每个条目:
将批次分隔为条目:
entries = batch_decoded_body.strip.split(/^--.*/)
第一个条目不是一个条目,而是一个标题,所以你可以摆脱它:
entries.shift
对于每个条目解析为JSON,从{
开始:
entries.map { |entry| JSON.parse entry[/{.*/m] }
所有在一起:
entries = batch_decoded_body.strip.split(/^--.*/)
entries.shift
entries.map { |entry| JSON.parse(entry[/{.*/m])['kind'] }
# => ["glass#timelineItem", "glass#timelineItem", "glass#timelineItem"]
entries.map { |entry| JSON.parse(entry[/{.*/m])['items'].map { |item| item['ipAddress'] } }
# => [["IP_ADDRESS_HERE"], ["IP_ADDRESS_HERE"]]
答案 1 :(得分:0)
您应该查看json库。例如:
require 'json'
require 'open-uri'
request = open('http://ip.jsontest.com')
response = JSON.parse(request.read)
response["ip"]
您可以使用JSON.parse来阅读您的回复(我假设它在一个名为response的变量中):
require 'json'
response_json = JSON.parse(response)
response_json['kind']
答案 2 :(得分:0)
如果您的回答与上面的回答完全相同,比如存储在一个名为str的变量中,并且您希望将这些类型作为字符串数组进行排序,则可以执行以下操作:
arr = str.scan(/\{.*?kind.*?\}/m)
puts arr