[英]ERLANG JSON decode error
我運行“ earTest:input(“ hai”,“ 1”,“ 0.1”)時收到以下錯誤。 在二郎殼上。 您能幫我一下嗎(我的編碼/解碼有問題嗎?)。
** exception error: no function clause matching xmerl_ucs:expand_utf8_1(
{obj,[{data,[{obj,[{"name","hai"},
{"number","1"},
{"marks","0.1"}]}]}]},
[],0
) (xmerl_ucs.erl, line 435)
in function xmerl_ucs:from_utf8/1 (xmerl_ucs.erl, line 183)
in call from rfc4627:unicode_decode/1 (rfc4627.erl, line 323)
in call from rfc4627:decode/1 (rfc4627.erl, line 258)
in call from erlTest:outputJ/1 (erlTest.erl, line 10)
碼:
-module(earTest).
-export([input/3]).
-import(rfc4627,[encode/1, decode/1]).
outputJ(X) ->
{ok, Json, _} = rfc4627:decode(X),
Airport = rfc4627:get_field(Json, "name", <<>>),
Airport.
input(X,Y,Z) ->
Data = [{obj,[{"name",X},{"number",Y},{"marks",Z}]}],
JsonData = {obj, [{data, Data}]},
rfc4627:encode(JsonData),
outputJ(JsonData).
您正在嘗試解碼未編碼的json,並創建了一個嵌套結構。
替換為
-module(earTest).
-export([input/3]).
-import(rfc4627,[encode/1, decode/1]).
outputJ(X) ->
{ok, Json, _} = rfc4627:decode(X),
[Inner_obj] = rfc4627:get_field(Json, "data", <<>>), % extract the inner object
Airport = rfc4627:get_field(Inner_obj, "name", <<>>),
Airport.
input(X,Y,Z) ->
% Here you are creating a list of one single object element
Data = [{obj,[{"name",X},{"number",Y},{"marks",Z}]}],
% and you put it in a "container" object, in the data field
JsonData = {obj, [{data, Data}]},
% you have to reuse the result of encoding in the decode function!
Res = rfc4627:encode(JsonData),
outputJ(Res).
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.