Erlang:JSON 列表到 JSON 列表

发布于 2024-09-26 07:36:32 字数 1006 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆流 2024-10-03 07:36:32

您可以使用lists:keydelete(Key, N, TupleList) 返回删除了某些元组的新元组列表。因此,在列表理解中,对于每个条目提取元组列表(或 proplists),并创建一个删除键的新结构:

B = [{struct, lists:keydelete(<<"name">>, 1, Props)} || {struct, Props} <- Struct].

给出:

[{struct,[{<<"id">>,<<"0129">>},
          {<<"photo">>,<<"joe.jpg">>}]},
 {struct,[{<<"id">>,<<"0759">>},
          {<<"photo">>,<<"jane.jpg">>}]},
 {struct,[{<<"id">>,<<"0929">>},
          {<<"photo">>,<<"john.jpg">>}]}]

iolist_to_binary(mochijson2:encode(B)).

给出:

<<"[{\"id\":\"0129\",\"photo\":\"joe.jpg\"},{\"id\":\"0759\",\"photo\":\"jane.jpg\"},{\"id\":\"0929\",\"photo\":\"john.jpg\"}]">>

顺便说一句,使用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:

B = [{struct, lists:keydelete(<<"name">>, 1, Props)} || {struct, Props} <- Struct].

gives:

[{struct,[{<<"id">>,<<"0129">>},
          {<<"photo">>,<<"joe.jpg">>}]},
 {struct,[{<<"id">>,<<"0759">>},
          {<<"photo">>,<<"jane.jpg">>}]},
 {struct,[{<<"id">>,<<"0929">>},
          {<<"photo">>,<<"john.jpg">>}]}]

and

iolist_to_binary(mochijson2:encode(B)).

gives:

<<"[{\"id\":\"0129\",\"photo\":\"joe.jpg\"},{\"id\":\"0759\",\"photo\":\"jane.jpg\"},{\"id\":\"0929\",\"photo\":\"john.jpg\"}]">>

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文