在Erlang中解析JSON

tho*_*s55 14 erlang json

我有一段JSON字符串,我想在Erlang中解析.看起来像:

({ id1 : ["str1", "str2", "str3"], id2 : ["str4", "str5"]})
Run Code Online (Sandbox Code Playgroud)

我查看了mochijson2和其他几个JSON解析器,但我真的无法弄清楚如何做到这一点.任何帮助非常感谢!

leg*_*cia 14

我曾经使用过erlang-json-eep-parser,并对你的数据进行了尝试.

7> json_eep:json_to_term("({ id1 : [\"str1\", \"str2\", \"str3\"], id2 : [\"str4\", \"str5\"]})").
** exception error: no match of right hand side value 
                    {error,{1,json_lex2,{illegal,"("}},1}
     in function  json_eep:json_to_term/1
Run Code Online (Sandbox Code Playgroud)

对,它不喜欢括号.

8> json_eep:json_to_term("{ id1 : [\"str1\", \"str2\", \"str3\"], id2 : [\"str4\", \"str5\"]}").
** exception error: no match of right hand side value 
                    {error,{1,json_lex2,{illegal,"i"}},1}
     in function  json_eep:json_to_term/1
Run Code Online (Sandbox Code Playgroud)

它不喜欢不带引号的键:

18> json_eep:json_to_term("{ \"id1\" : [\"str1\", \"str2\", \"str3\"], \"id2\" : [\"str4\", \"str5\"]}").
{[{<<"id1">>,[<<"str1">>,<<"str2">>,<<"str3">>]},
  {<<"id2">>,[<<"str4">>,<<"str5">>]}]}
Run Code Online (Sandbox Code Playgroud)

那看起来更好.

所以看起来你的数据几乎就是JSON,至少就这个解析器而言.