2017-06-30 39 views
0

我递归地解析地图以转换为json。这里是我的o输出fn:在Erlang中检测字符列表作为字符串

hash(Map) -> 
    binary:bin_to_list(jiffy:encode(Map)). 

atomJSON(Atom) -> binary:list_to_bin(atom_to_list(Atom)). 

% json output 
o(Number) when is_number(Number) -> 
    Number; 
o(Atom) when is_atom(Atom) -> 
    atomJSON(Atom); 
o(Tuple) when is_tuple(Tuple) -> 
    o(tuple_to_list(Tuple), []); 
o(List) when is_list(List) -> 
    o(List, []); 
o(Map) when is_map(Map) -> %expects atoms for internal map keys 
    o(maps:keys(Map), Map, #{}). 

o([], List) -> 
    lists:reverse(List); 
o([H | T], List) -> 
    o(T, [o(H) | List]). 

o([], _Map, Res) -> 
    hash(Res); 
o([H | T], Map, Res) -> 
    o(T, Map, maps:put(atomJSON(H), o(maps:get(H, Map)), Res)). 

其中大部分转换为JSON罚款。状态键&作为字符串的值不。

我知道erl shell方便地检测到一个字符串,但is_string/1不存在。如何检测字符串并将其转换,以便我可以在JavaScript中显示正确的文本?

enter image description here

2> [123, 34, 115, 116, 97, 116, 117, 115, 34, 58]. 
"{\"status\":" 
+0

你想在Erlang或JavaScript中检测到它吗?对于Erlang,你可以使用这个:https://stackoverflow.com/a/8035531/320615。 – Dogbert

+0

你好,狗狗!是的,看起来很有用。从这我可以计算出什么时候需要将我的字符列表转换为JSON可以作为字符串读取的内容。 – quantumpotato

+0

看起来我可以使用<>按照https://stackoverflow.com/questions/42044491/encoding-erlang-maps-as-json-with-strings-for-parsing-by-javascript – quantumpotato

回答

0

只需选中每个元素都值从8到255 整数,但我认为二郎做这项工作。所以也许原因是你阵列中的一些元素的值不在8到255的范围内。