json - 简化 Couchdb JSON 响应

标签 json couchdb transformation

我将位置数据存储在 Couchdb 中,并且正在寻找一种方法来获取仅包含值的数组,而不是每个记录的 key: value。例如:

目前的 react

{"total rows": 250, "offset": 0, "rows":[
    {"id": "ec5de6de2cf7bcac9a2a2a76de5738e4", "key": "user1", "value": {"city": "San Francisco", "address":"1001 Bayhill Dr"},
    {"id": "ec5de6de2cf7bcac9a2a2a76de573ae4","key": "user1", "value": {"city": "Palo Alto", "address":"583 Waverley St"}
    ... (etc).
]}

我只需要:
[{"city": "San Francisco", "address":"1001 Bayhill Dr"},
 {"city": "Palo Alto", "address":"583 Waverley St"},
 ...]

这一切的原因是为了最大限度地减少 JSON 响应消耗的带宽量。
我似乎找不到将 View 转换为简单数组的方法。有什么建议?

谢谢。

最佳答案

您可以使用 _show and _list函数,它们接受一个文档或一个 View (分别),并且可以以您需要的任何格式发回转换后的响应。 (在这种情况下,JSON)

更新:我使用您在此处提供的数据在我自己的 CouchDB 上运行了一个简单的测试。这是我最终编写的列表函数。自定义它以满足您的需求。 :)

function (head, req) {
    // specify that we're providing a JSON response
    provides('json', function() {
        // create an array for our result set
        var results = [];

        while (row = getRow()) {
            results.push({
                city: row.value.city,
                address: row.value.address
            });
        }

        // make sure to stringify the results :)
        send(JSON.stringify(results));
    });
}

关于json - 简化 Couchdb JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4949689/

相关文章:

couchdb - 我们可以为 couchdb 中的每个修订获得时间吗

c# - MatrixTransform 的矩阵在我看来是转置的

java - Spring转义已经转义的字符串

javascript - 无法通过 mandrillapp JSON api 将文档附加到电子邮件

json - Express:发送 JSON 文件作为响应

couchdb - 为什么 CouchDB View 行使用 "key": key, "value":value 而不是简单的 key:value

hadoop - Hadoop CouchDB Elasticsearch

javascript - JSON 解析和字符串化错误

c# - 将投影点映射到真实世界点

image - 如何进行这种图像转换?