我有一些旧的代码,我忘记了正确的json格式。
try
{L} = ejson:decode(JsonStr),
?INFO("json======>~w", [L]),
{_, {L1}} = lists:keyfind(<<"total">>, 1, L),
{_, Id} = lists:keyfind(<<"id">>, 1, L1),
{_, Name} = lists:keyfind(<<"name">>, 1, L1),
{_, IconId} = lists:keyfind(<<"icon_id">>, 1, L1),
{_, Title= lists:keyfind(<<"Title">>, 1, L1),
{_, L2} = lists:keyfind(<<"child">>, 1, L),..
Fun =
fun({L3}) ->
{_, Id1} = lists:keyfind(<<"id">>, 1, L3),
{_, Bid} = lists:keyfind(<<"bid">>, 1, L3),
{_, TotalId1} = lists:keyfind(<<"total_id">>, 1, L3),
...
end,...
下面是我的json格式:
'total'=>
[
'id' => 1,
'name' => 1,
'icon_id' => 503,
],
'child'=>
[
'id' => 1,
'group' => 0,
'total_id' => 20,
...]
但是解析JSON时出现错误,为什么会出现这个错误?
1条答案
按热度按时间o75abkj41#
If this is your actual JSON:
then it's malformed. You should check out the JSON spec . The long and the short of it is, JSON objects start and end with curly braces
{}
, keys are double-quoted""
and the separator between key and value is a colon:
.If you're trying to write ejson, that can be expressed as a map:
or a tuple/list thing in erlang terms. There's a really helpful representation in the jiffy README that I've copied here:
It looks like your parser is expecting parsing out the ejson into an set of erlang terms like this:
If your decoder has any ability to return a map, I would suggest you send that option along as they are MUCH MUCH easier to work with. If your decoder can't do that, then I would suggest you try and switch to using jiffy .