简化Couchdb JSON响应 [英] Simplify Couchdb JSON response

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

问题描述

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

I'm storing location data in Couchdb, and am looking for a way to get an array of just the values, instead of key: value for every record. For example:

当前响应

{"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响应消耗的带宽. 我似乎找不到一种将视图转换为简单数组的方法.有什么建议吗?

The reason for all this is to minimize the amount of bandwidth that a JSON response consumes. I can't seem to find a way to transform the view into a simple array. Any suggestions?

谢谢.

推荐答案

您可以使用 _show和_list 函数,它们分别获取文档或视图,并可以以您需要的任何格式发送回已转换的响应. (在这种情况下为JSON)

You can use _show and _list functions, they take either a document or a view (respectively) and can send back a transformed response in whatever format you need. (in this case, JSON)

更新:我对您在自己的CouchDB上提供的数据进行了简单测试.这是我最终编写的列表函数.对其进行自定义以满足您的需求. :)

Update: I ran a simple test with the data you provided here on my own CouchDB. Here's the list function I ended up writing. Customize it to fit your needs. :)

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));
    });
}

这篇关于简化Couchdb JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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