Decode JSON with mochijson2 in Erlang

后端 未结 5 915
灰色年华
灰色年华 2021-02-10 07:28

I have a var that has some JSON data:

A = <<\"{\\\"job\\\": {\\\"id\\\": \\\"1\\\"}}\">>. 

Using mochijson2, I decode the data:

5条回答
  •  时光说笑
    2021-02-10 08:13

    Here is another method of accessing the data. Uses records syntax for ease of use.

    -record(struct, {lst=[]}).
    
    A = <<"{\"job\": {\"id\": \"1\"}}">>,
    Struct = mochijson2:decode(A), 
    Job = proplists:get_value(<<"job">>, Struct#struct.lst),
    Id = proplists:get_value(<<"id">>, Job#struct.lst),
    

    Does exactly the same thing as the answer using records instead. Just another option when using mochijson2. I personally like this syntax better.

提交回复
热议问题