简体   繁体   English

如何在Erlang中解析“普通” JSON

[英]How to parse 'normal' JSON in Erlang

I'm writing a simple chat server in Erlang. 我正在用Erlang写一个简单的聊天服务器。 I want the server to receive commands from my client (An Android app written in Java). 我希望服务器从客户端(用Java编写的Android应用程序)接收命令。 One command consists of an JSON String, which includes the command itself and additional information which is needed to execute the command, eg 一个命令由一个JSON字符串组成,该字符串包括命令本身以及执行该命令所需的其他信息,例如

{"command": "send", "message": "Hello World!"}

So I've looked for Erlang JSON parsers, and stumbled upon jiffy, jsx and mochijson. 所以我一直在寻找Erlang JSON解析器,偶然发现了jiffy,jsx和mochijson。 The problem is that these parsers only accept 'Erlang Style JSON'(eg 问题在于这些解析器仅接受“ Erlang样式JSON”(例如

<<"{\"foo\": \"bar\"}">>

) , which would be fine if my client was written in Erlang, but don't understand 'normal' JSON, which is used by the JSON parser in my Android App. ),如果我的客户端是用Erlang编写的,那会很好,但是却不理解Android应用程序中JSON解析器使用的“普通” JSON。

Is there a way to use standard JSON with Erlang? 有没有办法在Erlang中使用标准JSON?

Your "erlang json" is just the way Erlang represents binary strings: 您的“ erlang json”就是Erlang表示二进制字符串的方式:

Try it yourself: 自己尝试:

$erl
1> list_to_binary(io:get_line("")).                          
{"command": "send", "message": "Hello World!"}
<<"{\"command\": \"send\", \"message\": \"Hello World!\"}\n">>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM