gpt4 book ai didi

json - Yojson 解析 int64 (ocaml)

转载 作者:行者123 更新时间:2023-12-03 23:13:54 26 4
gpt4 key购买 nike

根据我的理解,这个 {"xxx": 1000000000000000} 是有效的吗?

不确定如何使用 Yojson.Safe 解析它。我正在寻找类似 _ `Int64 of int64 _ 的东西,但没有提供,api 上只有 _ `Int of int _ 和 _ `Intlit of string _。

编辑,这是我的问题

let x = "{\"xxx\": 10000000000000}"
let json = Yojson.Safe.from_string x
match json with `Assoc [("xxx", `Intlit yyy)] -> yyy | _ -> assert false

不会匹配,因为json的类型是

val json : Yojson.Safe.json = `Assoc [("xxx", `Int 10000000000000)]

最佳答案

似乎 Yojson 返回 Int 如果它适合 OCaml int`Intlit 否则你需要处理所有情况:

match json with 
| `Assoc [("xxx", `Intlit lit)] -> Int64.of_string lit
| `Assoc [("xxx", `Int i)] -> Int64.of_int i

关于json - Yojson 解析 int64 (ocaml),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22246577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com