正在ruby中从批处理请求检索信息



这是一个示例批处理请求响应。

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的Batch Request得到了类似的响应。

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给出了上面的响应。变量batch_decoded中的OR表示我可以使用任意一个。

您需要分别处理批次中的每个条目:

  1. 单独批次到条目:

    entries = batch_decoded_body.strip.split(/^--.*/)
    
  2. 第一个条目实际上不是一个条目,而是一个标题,所以你可以去掉它:

    entries.shift
    
  3. 对于每个条目,解析为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"]] 

您可能应该查看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']

如果你的响应与上面的完全一样,比如存储在一个名为str的变量中,并且你想以字符串数组的形式导出种类,你可以执行以下操作:

arr = str.scan(/{.*?kind.*?}/m)
puts arr

相关内容

  • 没有找到相关文章

最新更新