【问题标题】:How to filter properties in JSON YQL request?如何过滤 JSON YQL 请求中的属性?
【发布时间】:2017-05-09 19:21:50
【问题描述】:

我正在使用 Yahoo Query Language (YQL) 从不支持 CORS 的 Web 服务中检索 JSON,并过滤每个响应对象的属性,以便响应 JSON 中的每个对象仅包含我需要的属性.

从 Web 服务返回的 JSON 如下所示:

{
    "results": [
        {},
        {},
        ...
    ]
}

我的查询目前如下所示:

select results from json where url="..."

这将返回 results 属性数组。如何修改“选择”查询或命令的其他部分以仅选择 results 数组中每个结果对象中的某些属性?我无法从documentation 中找到这个。

【问题讨论】:

    标签: json yql


    【解决方案1】:

    在您的情况下,您应该将查询设置为"results.name, results.number"

    让我们看一个请求一些占位符 JSON (https://jsonplaceholder.typicode.com/posts) 的示例:

    [
      {
        "userId": 1,
        "id": 1,
        "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
        "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
      },
      {
        "userId": 1,
        "id": 2,
        "title": "qui est esse",
        "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
      },
      ...
    ]
    

    在这个例子中,根是一个数组,我们可以使用这个 YQL 查询过滤数组中每个对象的属性:

    select json.userId, json.title from json where url='https://jsonplaceholder.typicode.com/posts'
    

    这给出了这个 JSON 响应:

          "results": {
           "json": [
            {
             "json": {
              "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
              "userId": "1"
             }
            },
            .
            .
            .
    

    【讨论】:

    • 嘿,Eric,如果数组不是从 Web 服务返回的 JSON 中的根值,而是一个对象的属性(该对象是根),您将如何处理?跨度>
    • 我认为这是我需要的 Eric,但我意识到我错过了一些东西。我编辑了问题以澄清我需要什么。
    • 我真的想通了。你的回答虽然有帮助。你仍然得到赏金。我将使用针对我的案例的正确查询来编辑您的答案:"results.name, results.number"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    相关资源
    最近更新 更多