【问题标题】:I am getting error parsing JSON. Why do I get that error?我在解析 JSON 时遇到错误。为什么我会收到这个错误?
【发布时间】:2022-01-07 16:41:06
【问题描述】:

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

【问题讨论】:

  • 可能是{"total": {"id":1, "name":1, "icon_id":503}, "child": {"id":1, "group":0, "total_id":20}, …}?

标签: json format erlang


【解决方案1】:

如果这是您的实际 JSON:

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

那么它的格式不正确。您应该查看JSON spec。总而言之,JSON 对象以花括号 {} 开头和结尾,键是双引号 "",键和值之间的分隔符是冒号 :

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

如果你想写ejson,那可以用map来表示:

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

或者 erlang 术语中的元组/列表。我在这里复制的jiffy README 中有一个非常有用的表示:

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">>}

您的解析器似乎希望将 ejson 解析为一组像这样的 erlang 术语:

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

如果您的解码器能够返回地图,我建议您发送该选项,因为它们更易于使用。如果您的解码器无法做到这一点,那么我建议您尝试改用jiffy

【讨论】:

  • 哦忘了说我在 php 上使用此代码数组/json 格式并将其发送到 erlang 服务器。当您在 php 中使用此代码时,我将获得与您相同的 json 格式 $array=[ 'total'=> [ 'id' => 1, 'name' => 1, 'titel' => 503,], '孩子'=> [ '茶' => 1, '姓名' => 1, '年龄' => 503,] ] ; print_r(array(json_encode($array)));不工作。所以我会重写我的erl文件并测试它
猜你喜欢
  • 2017-03-05
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多