先下载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才能作为字符串使用