Mochijson을 사용하여 Erlang에서 데이터 구조를 인코딩하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/1123589

  •  13-09-2019
  •  | 
  •  

문제

Mochiweb을 사용하고 있으며 JSON 인코더를 사용하여 복잡한 데이터 구조를 처리하는 방법을 모르겠습니다. Mochijson과 Mochijson2의 차이점은 무엇입니까? 좋은 예가 있습니까? 나는 항상 다음과 같은 오류를 얻습니다.

46> T6={struct,[{hello,"asdf"},{from,"1"},{to,{a,"aa"}}]}.
{struct,[{hello,"asdf"},{from,"1"},{to,{a,"aa"}}]}
47> mochijson2:encode(T6).                                
** exception exit: {json_encode,{bad_term,{a,"aa"}}}
     in function  mochijson2:json_encode/2
     in call from mochijson2:'-json_encode_proplist/2-fun-0-'/3
     in call from lists:foldl/3
     in call from mochijson2:json_encode_proplist/2


39> T3={struct,[{hello,"asdf"},{[{from,"1"},{to,"2"}]}]}.
{struct,[{hello,"asdf"},{[{from,"1"},{to,"2"}]}]}
40> mochijson:encode(T3).                                 
** exception error: no function clause matching mochijson:'-json_encode_proplist/2-fun-0-'({[{from,"1"},{to,"2"}]},
                                                                                           [44,"\"asdf\"",58,"\"hello\"",123],
                                                                                           {encoder,unicode,null})
     in function  lists:foldl/3
     in call from mochijson:json_encode_proplist/2
도움이 되었습니까?

해결책

Mochijson2는 문자열과 함께 바이너리, 배열로 목록으로 작동하며 UTF-8 만 디코딩합니다. Mochijson은 유니 코드 코드 포인트를 디코딩하고 인코딩합니다.

깊은 구조물을 만들려면 다음을 수행했습니다.

2> L = {struct, [{key2, [192]}]}. 
{struct,[{key2,"?"}]}
3> 
3> L2 = {struct, [{key, L}]}.   
{struct,[{key,{struct,[{key2,"?"}]}}]}
4> 
4> mochijson:encode(L2).
[123,"\"key\"",58,
 [123,"\"key2\"",58,[34,"\\u00c0",34],125],
 125]

따라서 목록을 사용하여 데이터 구조를 재귀 적으로 작성하면 괜찮을 것입니다. 나는 왜 깊은 구조가 지원되지 않는지 잘 모르겠지만, 그들은 적어도 당신이 그것들을 만들려고하는 방식이 아닌 것 같습니다. 어쩌면 다른 사람이 더 영리한 방법을 알고있을 것입니다.

이 스레드를 확인할 수도 있습니다. Mochijson2 예제!

또는

Erlang에서 웹 애플리케이션 개발을 시작하기위한 비디오 자습서

다른 팁

T6={struct, [{hello,"asdf"},{from,"1"},{to, {a,"aa"}} ]}.

대신 : 대신 : 대신 : 대신 : 대신 : 대신

T6={struct, [{hello,"asdf"},{from,"1"},{to, {struct, [{a,"aa"}]}} ]}.

중첩 구조는 최상위 객체와 동일한 "구조"스타일을 가져야합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top