在 KRL 中处理响应

发布于 2024-10-08 19:33:23 字数 246 浏览 0 评论 0原文

我正在使用 KRL 通过他们的 API 向 google 发送请求,这是我从他们那里得到的字面响应:

handleResponse({ "data": { "responses": [ { "response": "successful" } ] } } );

您建议我如何通过 pick 处理这个请求,因为它不是“有效”的 JSON 语法?它包含有效的 JSON 语法,但作为一个整体是无效的。感谢您的帮助。

I'm sending a request to google via their API using KRL and this is the literal response I am getting back from them:

handleResponse({ "data": { "responses": [ { "response": "successful" } ] } } );

How do you recommend I process this via pick as it is not 'valid' JSON syntax? It contains valid JSON syntax, but as a whole is not valid. Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莳間冲淡了誓言ζ 2024-10-15 19:33:23

更新:查看 Google 翻译 API 后,看起来 JSONP 回调参数是可选的。不要指定回调,您将不再遇到此问题。 :)

http://code.google.com/apis/language /translate/v2/using_rest.html#WorkingResults

更好的选择:

如果可以,请在对 google API 的调用中指定不存在回调函数。如果您可以只请求纯 JSON 而不是 JSONP,则可以仅使用 pick 运算符。

不是更好的选择:

如果 API 仅返回 JSONP,那么您可以执行正则表达式替换以从 JSON 中删除填充,然后您就可以使用选择运算符。

您需要什么:

完整的应用程序示例:

ruleset a60x494 {
  meta {
    name "jsonp-to-json-test"
    description <<
      jsonp-to-json-test
    >>
    author "Mike Grace"
    logging on
  }

  global {
    returnedJsonpAsString = 'handleResponse({ "data": { "responses": [ { "response": "successful" } ] } } );';
    datasource googleApi <- "blah blah blah";
  }

  rule fix_jsonp_to_json {
    select when pageview ".*"
    pre {
      cleanJson = returnedJsonpAsString.replace(re/^.*\((.*)\);/,"$1");
      response = cleanJson.decode().pick("$..response");
    }
    {
      notify("Response",response) with sticky = true;
      emit <|
        console.log(returnedJsonp);
        console.log(cleanJson);
      |>;
    }
  }
}

Update: After looking at the Google translate API it looks like the JSONP callback parameter is optional. Don't specify a callback and you will no longer have this issue. : )

http://code.google.com/apis/language/translate/v2/using_rest.html#WorkingResults

Better option:

If you can, specify in your call to the google API that there be no callback function. If you can just request plain JSON instead of JSONP you can just use the pick operator.

Not so better option:

If the API only returns JSONP then you can do a regex replace to remove the padding from the JSON which will then allow you to use the pick operator.

What you'll need:

Full app example:

ruleset a60x494 {
  meta {
    name "jsonp-to-json-test"
    description <<
      jsonp-to-json-test
    >>
    author "Mike Grace"
    logging on
  }

  global {
    returnedJsonpAsString = 'handleResponse({ "data": { "responses": [ { "response": "successful" } ] } } );';
    datasource googleApi <- "blah blah blah";
  }

  rule fix_jsonp_to_json {
    select when pageview ".*"
    pre {
      cleanJson = returnedJsonpAsString.replace(re/^.*\((.*)\);/,"$1");
      response = cleanJson.decode().pick("$..response");
    }
    {
      notify("Response",response) with sticky = true;
      emit <|
        console.log(returnedJsonp);
        console.log(cleanJson);
      |>;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文