首页 > 代码库 > erlang string to term   如何把字符串动态解析成为Erlang数据结构

erlang string to term   如何把字符串动态解析成为Erlang数据结构

list_to_term(String) ->
    {ok, T, _} = string(String++"."),%%String加上一个英文句号(或者点号)
    case parse_term(T) of
        {ok, Term} ->
            Term        {error, Error} ->
            Error
    end.



Eshell V5.8.2 (abort with ^G)
1> {ok, Scan1, _} = erl_scan:string("[a,b,c].").
{ok,[{‘[‘,1},{atom,1,a},{‘,‘,1},{atom,1,b},{‘,‘,1},{atom,1,c},{‘]‘,1},{dot,1}],1}
2> {ok,P}=erl_parse:parse_exprs(Scan1).
{ok,[{cons,1,{atom,1,a},{cons,1,{atom,1,b},{cons,1,{atom,1,c},{nil,1}}}}]}
3> erl_eval:exprs(P, []) .





本文出自 “一天一点努力” 博客,请务必保留此出处http://java711.blog.51cto.com/1786533/1910021

erlang string to term   如何把字符串动态解析成为Erlang数据结构