简体   繁体   English

Erlang:在Json中查找数据

[英]Erlang: Find data in Json

I have one predefined set of keys key1,key2 ...key10. 我有一组预定义的键key1,key2 ... key10。 (These keys will not change) I need to extract data from json corresponding to these keys. (这些键不会改变)我需要从与这些键相对应的json中提取数据。 For example access, token, expires at: 例如, access, token, expires在以下时间access, token, expires

{\"access\": {\"token\": {\"issued_at\": \"2015-09-10T12:03:49.554141\", \"expires\": \"2015-09-10T13:03:49Z\" \"id\": \"dbb60c28daf34b80905883789f698cde\", \"tenant\": {\"description\": null, \"enabled\": true,

How can I extract the value for these keys not using json libraries? 不使用json库,如何提取这些键的值? Is it a good idea? 这是个好主意吗? (I think that overhead of parsing entire string doesn`t worth) (我认为解析整个字符串的开销不值得)

How big is JSON we are talking about? 我们正在谈论的JSON有多大? Is this JSON fed by chunks or is already loaded whole in the memory? 这个JSON是由块填充还是已经全部加载到内存中? If yes, why bother? 如果是,为什么要打扰? How fast is fast enough for you? 多快才适合您? There is a whole bunch of question to ask before you start solving it. 在开始解决之前,有很多问题要问。 In most cases parsing whole JSON should be sufficient: 在大多数情况下,解析整个JSON就足够了:

get_from_json(JSON, Path) ->
    path(jiffy:decode(JSON), Path).

path(Obj, []) -> Obj;
path({List}, [Key|Path]) when is_list(List) ->
    case lists:keyfind(iolist_to_binary(Key), 1, List) of
        false -> not_found;
        {_, Obj} -> path(Obj, Path)
    end;
path(_, _) -> not_found.

Otherwise, you can write your own parser. 否则,您可以编写自己的解析器。 You can even use leex or existing JSON parser like jsx as starting point. 您甚至可以使用leex或现有的JSON解析器(如jsx作为起点。

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

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