在Erlang中解析JSON [英] Parsing JSON in Erlang

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

问题描述

我有一块JSON字符串,我想在Erlang中解析。它看起来像:

 ({id1:[str1,str2,str3],id2:[ str4,str5]})

我看了mochijson2,还有一些其他JSON解析器,但我真的不知道该怎么做。任何帮助非常感谢!

解决方案

我曾经使用 erlang-json-eep-parser ,并尝试使用您的数据。

  7个json_eep:json_to_term(({id1:[\str1\,\str2\,\str3\],id2:[\str4\,\str5 \ ]}))。 
**异常错误:右侧不匹配
{error,{1,json_lex2,{illegal,(}}} 1}
在函数json_eep中:json_to_term / 1

对,它不喜欢括号。

  8> json_eep:json_to_term({id1:[\str1\,\str2\,\str3\],id2 :[\str4\,\str5\]})
**异常错误:右侧值不匹配
{error,{1,json_lex2, json_eep:json_to_term / 1

它不喜欢无引号的键:

  18> json_eep:json_to_term({\id1\ :[\str1\,\str2\,\str3\],\id2\:[\str4\,\str5\ ]})。
{[{<id1>,<str1>,<str2> <str3>>]},
{<id2&g t;>,[<str4>><str5>>]}]}

看起来更好。



所以,似乎你的数据几乎是 JSON,至少就这个解析器而言。


I have a piece of JSON string, which I want to parse in Erlang. It looks like:

({ id1 : ["str1", "str2", "str3"], id2 : ["str4", "str5"]})

I looked at mochijson2, and a couple of other JSON parsers, but I really could not figure out how to do it. Any help greatly appreciated!

解决方案

I once used the erlang-json-eep-parser, and tried it on your data.

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

Right, it doesn't like the parentheses.

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

And it doesn't like the unquoted keys:

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

That looks better.

So it seems that your data is almost JSON, at least as far as this parser is concerned.

这篇关于在Erlang中解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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