3

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

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

编辑,这是我的问题

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)]
4

1 回答 1

3

如果YojsonInt适合 OCaml int,它似乎会返回,`Intlit否则您需要处理所有情况:

match json with 
| `Assoc [("xxx", `Intlit lit)] -> Int64.of_string lit
| `Assoc [("xxx", `Int i)] -> Int64.of_int i
于 2014-03-07T10:19:05.827 回答