Erlang:JSON 列表到 JSON 列表
我有一个 JSON 对象列表(从 nosql 数据库接收)并且想要删除或重命名一些键。然后我想再次以 JSON 对象列表的形式返回数据。
这篇 Stackoverflow 帖子很好地介绍了如何使用 mochijson2 。我想我可以使用 列表理解 来完成JSON 对象的列表。
我遇到的问题是如何在列表理解中删除每个 JSON 对象(或 proplist,如果使用 mochijson2)的键。我可以使用 proplists 的删除功能。但当我尝试在列表理解中做到这一点时,我没有成功。
这是上下文的一些代码:
A = <<"[{\"id\": \"0129\", \"name\": \"joe\", \"photo\": \"joe.jpg\" }, {\"id\": \"0759\", \"name\": \"jane\", \"photo\": \"jane.jpg\" }, {\"id\": \"0929\", \"name\": \"john\", \"photo\": \"john.jpg\" }]">>.
Struct = mochijson2:decode(A).
{struct, JsonData} = Struct,
{struct, Id} = proplists:get_value(<<"id">>, JsonData),
非常感谢用代码说明的任何建议。
I have a list of JSON objects (received from a nosql db) and want to remove or rename some keys. And then I want to return data as a list of JSON objects once again.
This Stackoverflow post provides a good sense of how to use mochijson2. And I'm thinking I could use a list comprehension to go through the list of JSON objects.
The part I am stuck with is how to remove the key for each JSON object (or proplist if mochijson2 is used) within a list comprehension. I can use the delete function of proplists. But I am unsuccessful when trying to do that within a list comprehension.
Here is a bit code for context :
A = <<"[{\"id\": \"0129\", \"name\": \"joe\", \"photo\": \"joe.jpg\" }, {\"id\": \"0759\", \"name\": \"jane\", \"photo\": \"jane.jpg\" }, {\"id\": \"0929\", \"name\": \"john\", \"photo\": \"john.jpg\" }]">>.
Struct = mochijson2:decode(A).
{struct, JsonData} = Struct,
{struct, Id} = proplists:get_value(<<"id">>, JsonData),
Any suggestions illustrated with code much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用lists:keydelete(Key, N, TupleList) 返回删除了某些元组的新元组列表。因此,在列表理解中,对于每个条目提取元组列表(或 proplists),并创建一个删除键的新结构:
给出:
和
给出:
顺便说一句,使用lists / *元组列表函数要快得多,但是有时比 proplists/* 函数稍微不太方便: http ://sergioveiga.com/index.php/2010/05/14/erlang-listskeyfind-vs-proplistsget_value/
You can use lists:keydelete(Key, N, TupleList) to return a new tuple list with certain tuples removed. So in the list comprehension, for each entry extract the tuple lists (or proplists), and create a new struct with the key removed:
gives:
and
gives:
By the way, using the lists/* tuple lists functions are much faster, but sometimes slightly less convenient than the proplists/* functions: http://sergioveiga.com/index.php/2010/05/14/erlang-listskeyfind-vs-proplistsget_value/