简化 Couchdb JSON 响应
我将位置数据存储在 Couchdb 中,并且正在寻找一种方法来获取仅包含值的数组,而不是每条记录的 key: value 。例如:
当前响应
{"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 响应消耗的带宽量。 我似乎找不到一种方法将视图转换为简单的数组。有什么建议吗?
谢谢。
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:
The current response
{"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).
]}
I only really need:
[{"city": "San Francisco", "address":"1001 Bayhill Dr"},
{"city": "Palo Alto", "address":"583 Waverley St"},
...]
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?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 _show 和 _list 函数,它们分别获取文档或视图,并且可以以您需要的任何格式发回转换后的响应。 (在本例中为 JSON)
更新:我使用您在我自己的 CouchDB 上提供的数据进行了简单的测试。这是我最终编写的列表函数。定制它以满足您的需求。 :)
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)
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. :)