2016-10-29 4 views
0

"earTest : input ("hai ","1 ","0.1 ")을 실행하면 다음 오류가 발생합니다. 에를 랑 껍질에. 당신이 나를 도울 수 pls .. (내 인코딩/디코딩과 어떤 문제?).ERLANG JSON 디코딩 오류

** 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). 
+0

입력 (X, Y, Z)로 대체 -> 데이터 = {OBJ [{ "이름"X}, { "숫자", Y}, { "마크" Z}]}], JsonData = {obj, [{data, Data}]}, rfc4627 : encode (JsonData), outputJ (JsonData). – murty

+0

데이터를 인코딩 한 후 "input"함수에서 "outputJ (X)"함수를 호출하는 중 ... 인코딩 및 디코드를 실행하고 내용을 검색하려면 ... (들여 쓰기로 인한 일부 문제에 직면했을 때 죄송합니다.) – murty

답변

1

당신은 비 인코딩 된 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). 
+0

선생님, 고맙지 만 런타임 오류가 계속 발생합니다 .. 13> c (earTest). {ok, earTest} 14> earTest : input ("123", "345", "678"). ** 예외 오류 : function errorTest : outputJ/1 (earTest.erl, line 7) 줄 7 : [Inner_obj] = rfc4627 : get_field (Json, "Data", <<>)의 예외 값 : 오른쪽 오류 값과 일치하지 않음 <<>> >), % 내부 객체를 추출하십시오. – murty

+0

CHARLIE, 그 작업 후 "Data"를 "data"로 변경했습니다. [Inner_obj] = rfc4627 : get_field (Json, "data", <<>>) – murty

+0

오타를 사용하여 죄송합니다. 보내기 전에 코드를 테스트하지 않았습니다. 정확성에 대한 답을 편집합니다. – Pascal