简体   繁体   English

将 Erlang 元组编码为 JSON

[英]Encoding Erlang Tuple as JSON

How do I convert a list with Tuples & Atoms & Binary strings in a list into JSON?如何将列表中包含元组、原子和二进制字符串的列表转换为 JSON? I see Erlang : Tuple List into JSON and I found https://github.com/rustyio/BERT-JS我看到Erlang : Tuple List into JSON ,我发现https://github.com/rustyio/BERT-JS

I want an API I can call like我想要一个我可以调用的 API

erlang_json:convert([{a, b, {{c, d}}, 1}, {"a", "b", {{cat, dog}}, 2}

where the atoms would be converted to strings or some other standard way to process on the Javascript side.原子将被转换为字符串或其他一些标准方式在 Javascript 端进行处理。

I have complicated Erlang lists I need to send to my webpage.我有复杂的 Erlang 列表需要发送到我的网页。

It's unclear what [{a, b, {{c, d}}, 1}, {"a", "b", {{cat, dog}}, 2}... would turn into as JSON, but you might take a look at jiffy or jsx .目前还不清楚[{a, b, {{c, d}}, 1}, {"a", "b", {{cat, dog}}, 2}...会变成 JSON,但是你可能会看看jiffyjsx Both of them work on simple key/value structures.它们都适用于简单的键/值结构。 For instance:例如:

> Term = #{a => b, c => 1, <<"x">> => <<"y">>}.
#{a => b,c => 1,<<"x">> => <<"y">>}

> jiffy:encode(Term).
<<"{\"x\":\"y\",\"c\":1,\"a\":\"b\"}">>

> jsx:encode(Term).
<<"{\"a\":\"b\",\"c\":1,\"x\":\"y\"}">>

If you can say what JSON you want your example input to turn into, I might be able to give you a better suggestion.如果你能说出你想让你的示例输入变成什么 JSON,我可能会给你一个更好的建议。

Just for you https://github.com/romanr321/t2j只为你https://github.com/romanr321/t2j

You don't need to wrap it in a list though, it takes one tuple argument and returnes a json formated string.你不需要将它包装在一个列表中,它需要一个元组参数并返回一个 json 格式的字符串。

>Tuple = {{key, value}, { key2, {key3, [value1, 2,3]}}}.
>t2j:t2jp(Tuple).
{"key":"value", "key2, {"key3":["value1", 2,3]}}

The library jsone is pretty good.图书馆jsone非常好。 It can translate between maps or tuples: https://github.com/sile/jsone它可以在映射或元组之间进行转换: https : //github.com/sile/jsone

I've used it extensively and it's lightning fast.我已经广泛使用它,而且速度快如闪电。

The only problem I've found is that a map that contains a list of maps throws an error.我发现的唯一问题是包含地图列表的地图会引发错误。 I hope this is fixed, but maybe I'm the only tart trying to do that.我希望这是固定的,但也许我是唯一一个试图这样做的人。

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

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