Json编码,在python里就是一句话:
json.dumps(user_data)
但是Erlang没有自带的Json模块,只能自己造轮子:
先下载mochijson2:
https://github.com/mochi/mochiweb/blob/master/src/mochijson2.erl
用erlc编译
然后,上代码:
get_json_data() -> UserData = [{"product_name", "Programming Erlang"},{"order_type", "97"},{"money","100"}], DataConverted = [{X,list_to_binary(Y)} || {X,Y} <- UserData],%%convert value to binary O1 = mochijson2:encode(DataConverted), O2 = iolist_to_binary(O1), binary_to_list(O2).%%format to string
1. encode之前,需要将value转为binary编码
2. encode之后的数据是io_list, 要先转为binary,再转为list才能作为字符串使用
[Erlang]如何在Erlang中将对list进行json编码?,布布扣,bubuko.com
[Erlang]如何在Erlang中将对list进行json编码?
原文:http://blog.csdn.net/u012973744/article/details/29601995