什么相当于在Nim中将JSON加载到dict中

wis*_*shi 2 json nim-lang

我是Nim的新手,我想将JSON文件读取到dict样式数据结构,以便将键作为列访问.

proc get_url() : string =
    return "train.json"
Run Code Online (Sandbox Code Playgroud)

和解析器:

var file = get_url()
var json_data = file.parseFile()
var values = newTable() # dict style data structure?

for record in json_data:
 for key, value in record:
   values[key] = value # not
Run Code Online (Sandbox Code Playgroud)

显然,表需要更具体地实例化.

我不知道JSON文件中的密钥数量.在手册中,它的例子如下var a = {"hi": 1, "there": 2}.toTable.这似乎不足以解析随机JSON文件.

dom*_*m96 6

使用stdlib的json模块解析json 已经为您提供了一种dict风格的数据结构:JsonNode.

json文档介绍了如何使用这个相当好:https://nim-lang.org/docs/json.html#dynamically-retrieving-fields-from-json