erlang 解析JSON时出现错误,为什么会出现该错误?

brjng4g3  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(200)

我有一些旧的代码,我忘记了正确的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时出现错误,为什么会出现这个错误?

o75abkj4

o75abkj41#

If this is your actual JSON:

'total'=>
    [
    'id' => 1,
    'name' => 1,
    'icon_id' => 503,
    ],
    'child'=>
    [
    'id' => 1,
    'group' => 0,
    'total_id' => 20,
  ...]

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 : .

{
    "total" : {
      "id" : 1,
      "name" : 1,
      "icon_id" : 503
    },
    "child" :
    {
      "id" : 1,
      "group" : 0,
      "total_id" : 20
    } ...
  }

If you're trying to write ejson, that can be expressed as a map:

#{
  <<"total">> => #{
    <<"id">> => 1,
    <<"name">> => 1,
    <<"icon_id">> => 503
  },
  <<"child">> => #{
    <<"id">> => 1,
    <<"group">> => 0,
    <<"total_id">> => 20
   }..
}

or a tuple/list thing in erlang terms. There's a really helpful representation in the jiffy README that I've copied here:

Erlang                          JSON            Erlang
==========================================================================
null                       -> null           -> null
true                       -> true           -> true
false                      -> false          -> false
"hi"                       -> [104, 105]     -> [104, 105]
<<"hi">>                   -> "hi"           -> <<"hi">>
hi                         -> "hi"           -> <<"hi">>
1                          -> 1              -> 1
1.25                       -> 1.25           -> 1.25
[]                         -> []             -> []
[true, 1.0]                -> [true, 1.0]    -> [true, 1.0]
{[]}                       -> {}             -> {[]}
{[{foo, bar}]}             -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
{[{<<"foo">>, <<"bar">>}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
#{<<"foo">> => <<"bar">>}  -> {"foo": "bar"} -> #{<<"foo">> => <<"bar">>}

It looks like your parser is expecting parsing out the ejson into an set of erlang terms like this:

{[{<<"foo">>, <<"bar">>}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}

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 .

相关问题