用Javascript解析Gmail批处理响应 [英] Parsing Gmail Batch response in Javascript

查看:56
本文介绍了用Javascript解析Gmail批处理响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript调用/batch API方法来一次获取大量消息.根据文档,它返回具有多部分/混合内容类型的HTTP响应.我正在尝试将其作为JSON循环,但不确定如何将其转换.任何帮助将不胜感激.谢谢!

I'm using javascript to call the /batch API method for getting a number of messages at once. According to the docs it returns an HTTP response with a multipart/mixed content type. I'm trying to loop through this as JSON, but am not sure how to convert it. Any help would be greatly appreciated. Thanks!

推荐答案

我写了小型图书馆.您可以使用它,或者从代码中获得一些启发:

I have written a tiny library for this. You could use that or maybe get some inspiration from the code:

function parseBatchResponse(response) {
  // Not the same delimiter in the response as we specify ourselves in the request,
  // so we have to extract it.
  var delimiter = response.substr(0, response.indexOf('\r\n'));
  var parts = response.split(delimiter);
  // The first part will always be an empty string. Just remove it.
  parts.shift();
  // The last part will be the "--". Just remove it.
  parts.pop();

  var result = [];
  for (var i = 0; i < parts.length; i++) {
    var part = parts[i];
    var p = part.substring(part.indexOf("{"), part.lastIndexOf("}") + 1);
    result.push(JSON.parse(p));
  }
  return result;
}

这篇关于用Javascript解析Gmail批处理响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆