如何使用Yojson迭代JSON对象中的键/成员?

如何使用Yojson迭代JSON对象中的键/成员?,json,key,ocaml,Json,Key,Ocaml,到目前为止,我所能看到的关于处理json对象的唯一例子涉及到预先知道键,但是我如何能够遍历键/成员并单独处理它们的值呢?假设您已经创建了一个json实例,可能是通过使用Yojson.Basic.from_通道(在文件名中打开_)从文件中读取或从使用Yojson.Basic.from_string string的字符串中,您可以使用Yojson.Basic.Util.to_assoc将其转换为关联列表,并递归地对其进行如下迭代: open Yojson.Basic let iter js =

到目前为止,我所能看到的关于处理json对象的唯一例子涉及到预先知道键,但是我如何能够遍历键/成员并单独处理它们的值呢?

假设您已经创建了一个
json
实例,可能是通过使用
Yojson.Basic.from_通道(在文件名中打开_)从文件中读取
或从使用
Yojson.Basic.from_string string
的字符串中,您可以使用
Yojson.Basic.Util.to_assoc
将其转换为关联列表,并递归地对其进行如下迭代:

open Yojson.Basic

let iter js =
  let rec f (nm,j) =
    Printf.printf "\"%s\": " nm;
    match j with
    | `Assoc a ->
       Printf.printf "assoc\n";
       List.iter f a
    | `Bool b ->
       Printf.printf "bool: %b\n" b
    | `Float f ->
       Printf.printf "float: %f\n" f
    | `Int i ->
       Printf.printf "int: %d\n" i
    | `List jl ->
       Printf.printf "list: [";
       List.iter (List.iter f) (List.map Util.to_assoc jl);
       Printf.printf "]\n"
    | `Null ->
       Printf.printf "null\n"
    | `String s ->
       Printf.printf "string: \"%s\"\n" s in
  List.iter f (Util.to_assoc js)

iter
函数首先将其
js
参数转换为关联列表,然后使用函数
f
对其进行迭代。
f
函数接受字符串的元组参数和
json
类型。在本例中,它打印字符串,然后匹配
json
类型并相应地对其进行操作。请注意
f
如何递归地处理
`Assoc
`List
变量的值。

假设您已经创建了一个
json
实例,可以通过使用
Yojson.Basic.from_通道(在文件名中打开_)
从文件读取,或者使用
Yojson.Basic.from_字符串
从字符串读取,然后,您可以使用
Yojson.Basic.Util.to_assoc
将其转换为关联列表,并递归地对其进行如下迭代:

open Yojson.Basic

let iter js =
  let rec f (nm,j) =
    Printf.printf "\"%s\": " nm;
    match j with
    | `Assoc a ->
       Printf.printf "assoc\n";
       List.iter f a
    | `Bool b ->
       Printf.printf "bool: %b\n" b
    | `Float f ->
       Printf.printf "float: %f\n" f
    | `Int i ->
       Printf.printf "int: %d\n" i
    | `List jl ->
       Printf.printf "list: [";
       List.iter (List.iter f) (List.map Util.to_assoc jl);
       Printf.printf "]\n"
    | `Null ->
       Printf.printf "null\n"
    | `String s ->
       Printf.printf "string: \"%s\"\n" s in
  List.iter f (Util.to_assoc js)

iter
函数首先将其
js
参数转换为关联列表,然后使用函数
f
对其进行迭代。
f
函数接受字符串的元组参数和
json
类型。在本例中,它打印字符串,然后匹配
json
类型并相应地对其进行操作。请注意
f
如何递归地处理
`Assoc
`List
变量的值。

Yojson被设计为作为的运行时。Atdgen通过将JSON数据转换为OCaml类型的数据(反之亦然),为用户节省了大量时间

如下所示(示例源文件
assoc.atd
):

此示例生成的接口为:

(* This is file assoc_j.mli derived from assoc.atd. It provides the serializers and deserializers for JSON. Other files are generated for other purposes, including assoc_t.mli which contains only the OCaml type definitions. *) type foo = Assoc_t.foo = { x: int; y: int } type t = Assoc_t.t val write_foo : Bi_outbuf.t -> foo -> unit (** Output a JSON value of type {!foo}. *) val string_of_foo : ?len:int -> foo -> string (** Serialize a value of type {!foo} into a JSON string. @param len specifies the initial length of the buffer used internally. Default: 1024. *) val read_foo : Yojson.Safe.lexer_state -> Lexing.lexbuf -> foo (** Input JSON data of type {!foo}. *) val foo_of_string : string -> foo (** Deserialize JSON data of type {!foo}. *) val write_t : Bi_outbuf.t -> t -> unit (** Output a JSON value of type {!t}. *) val string_of_t : ?len:int -> t -> string (** Serialize a value of type {!t} into a JSON string. @param len specifies the initial length of the buffer used internally. Default: 1024. *) val read_t : Yojson.Safe.lexer_state -> Lexing.lexbuf -> t (** Input JSON data of type {!t}. *) val t_of_string : string -> t (** Deserialize JSON data of type {!t}. *) (*这是从assoc.atd派生的文件assoc_j.mli。 它提供JSON的序列化程序和反序列化程序。 其他文件是为其他目的生成的, 包括仅包含OCaml的assoc_t.mli 类型定义。 *) 类型foo=Assoc_t.foo={x:int;y:int} 类型t=关联t.t val write_foo: Bi_EXBUF.t->foo->unit (**输出类型为{!foo}.*的JSON值) val string_of_foo: ?len:int->foo->string (**序列化类型为{!foo}的值 转换为JSON字符串。 @param len指定初始长度 内部使用的缓冲区的名称。 默认值:1024.*) 瓦尔·雷德福: Yojson.Safe.lexer_state->Lexing.lexbuf->foo (**输入类型为{!foo}.*的JSON数据) 字符串的val foo_: 字符串->foo (**反序列化类型为{!foo}.*的JSON数据) val写入: Bi_exputf.t->t->unit (**输出类型为{!t}.*的JSON值) val字符串(共个): ?len:int->t->string (**序列化类型为{!t}的值 转换为JSON字符串。 @param len指定初始长度 内部使用的缓冲区的名称。 默认值:1024.*) val read\t: Yojson.Safe.lexer_state->Lexing.lexbuf->t (**输入类型为{!t}.*的JSON数据) 字符串的val t_: 字符串->t (**反序列化{!t}.*类型的JSON数据)
Yojson被设计为作为的运行时。Atdgen通过将JSON数据转换为OCaml类型的数据(反之亦然),为用户节省了大量时间

如下所示(示例源文件
assoc.atd
):

此示例生成的接口为:

(* This is file assoc_j.mli derived from assoc.atd. It provides the serializers and deserializers for JSON. Other files are generated for other purposes, including assoc_t.mli which contains only the OCaml type definitions. *) type foo = Assoc_t.foo = { x: int; y: int } type t = Assoc_t.t val write_foo : Bi_outbuf.t -> foo -> unit (** Output a JSON value of type {!foo}. *) val string_of_foo : ?len:int -> foo -> string (** Serialize a value of type {!foo} into a JSON string. @param len specifies the initial length of the buffer used internally. Default: 1024. *) val read_foo : Yojson.Safe.lexer_state -> Lexing.lexbuf -> foo (** Input JSON data of type {!foo}. *) val foo_of_string : string -> foo (** Deserialize JSON data of type {!foo}. *) val write_t : Bi_outbuf.t -> t -> unit (** Output a JSON value of type {!t}. *) val string_of_t : ?len:int -> t -> string (** Serialize a value of type {!t} into a JSON string. @param len specifies the initial length of the buffer used internally. Default: 1024. *) val read_t : Yojson.Safe.lexer_state -> Lexing.lexbuf -> t (** Input JSON data of type {!t}. *) val t_of_string : string -> t (** Deserialize JSON data of type {!t}. *) (*这是从assoc.atd派生的文件assoc_j.mli。 它提供JSON的序列化程序和反序列化程序。 其他文件是为其他目的生成的, 包括仅包含OCaml的assoc_t.mli 类型定义。 *) 类型foo=Assoc_t.foo={x:int;y:int} 类型t=关联t.t val write_foo: Bi_EXBUF.t->foo->unit (**输出类型为{!foo}.*的JSON值) val string_of_foo: ?len:int->foo->string (**序列化类型为{!foo}的值 转换为JSON字符串。 @param len指定初始长度 内部使用的缓冲区的名称。 默认值:1024.*) 瓦尔·雷德福: Yojson.Safe.lexer_state->Lexing.lexbuf->foo (**输入类型为{!foo}.*的JSON数据) 字符串的val foo_: 字符串->foo (**反序列化类型为{!foo}.*的JSON数据) val写入: Bi_exputf.t->t->unit (**输出类型为{!t}.*的JSON值) val字符串(共个): ?len:int->t->string (**序列化类型为{!t}的值 转换为JSON字符串。 @param len指定初始长度 内部使用的缓冲区的名称。 默认值:1024.*) val read\t: Yojson.Safe.lexer_state->Lexing.lexbuf->t (**输入类型为{!t}.*的JSON数据) 字符串的val t_: 字符串->t (**反序列化{!t}.*类型的JSON数据)