如何在包 Yojson Ocaml 中创建新类型
how create new type in package Yojson Ocaml
我有 json 个文件 test.json
{"response":[
{
"aid":209228783,
"thumb_id":"348954492",
"owner_id":-79421906,
"title":"title1",
"description":"description1",
"created":"1420821911",
"updated":"1421783832",
"size":284,
"can_upload":1
},
{
"aid":205134660,
"thumb_id":"353880937",
"owner_id":-79421906,
"title":"title2",
"description":"description2",
"created":"1415386976",
"updated":"1425057394",
"size":308,
"can_upload":0
},
{
"aid":204502901,
"thumb_id":"347548800",
"owner_id":-79421906,
"title":"title3",
"description":"description3",
"created":"1414438611",
"updated":"1419706388",
"size":1030,
"can_upload":0
}
]}
示例,需要从 json 文件中获取值 "aid"、"description" 和 "size"。
我创建新类型响应
type response = {
aid: int;
thumb_id: string;
owner_id: int;
title: string;
description: string;
created: string;
updated: string;
size: int;
can_upload: int
}
我如何使用它是在我的 json 请求中输入
let des json =
[json]
|> filter_member "response"
|> flatten
|> to_list;;
let json = Yojson.Basic.from_file "test.json" in
List.iter (fun y -> print_endline(y.aid^"--->"^y.title)) (des json);;
此代码 return 错误类型错误。
(将您的回复记录缩小一点,但仍能说明问题)
type response = {descr:string;
size: int;
can_upload: int}
let record_list =
let open Yojson.Basic.Util in
let f = Yojson.Basic.from_file "test.json" in
List.fold_right
(fun item r_list ->
{descr = member "description" item |> to_string;
size = member "size" item |> to_int;
can_upload = member "can_upload" item |> to_int} :: r_list)
(member "response" f |> to_list)
[]
Yojson 实际上只是 atdgen 的运行时。将类型定义放入 .atd
文件中,atdgen 将使用 yojson:
为您生成样板代码
(* This is file api.atd *)
type response_item = {
aid: int;
thumb_id: string;
owner_id: int;
title: string;
description: string;
created: string;
updated: string;
size: int;
can_upload: int
}
type response = {
response: response_item list;
}
Atdgen 生成文件 api_t.mli
、api_t.ml
、api_j.mli
和 api_j.ml
,其中 _t
代表类型,_j
代表JSON。请参阅有关如何将 atdgen 与您的构建系统集成的教程。
然后根据您的 OCaml 代码,您所要做的就是:
let record_list =
Ag_util.Json.from_file Api_j.read_response "test.json"
我有 json 个文件 test.json
{"response":[
{
"aid":209228783,
"thumb_id":"348954492",
"owner_id":-79421906,
"title":"title1",
"description":"description1",
"created":"1420821911",
"updated":"1421783832",
"size":284,
"can_upload":1
},
{
"aid":205134660,
"thumb_id":"353880937",
"owner_id":-79421906,
"title":"title2",
"description":"description2",
"created":"1415386976",
"updated":"1425057394",
"size":308,
"can_upload":0
},
{
"aid":204502901,
"thumb_id":"347548800",
"owner_id":-79421906,
"title":"title3",
"description":"description3",
"created":"1414438611",
"updated":"1419706388",
"size":1030,
"can_upload":0
}
]}
示例,需要从 json 文件中获取值 "aid"、"description" 和 "size"。 我创建新类型响应
type response = {
aid: int;
thumb_id: string;
owner_id: int;
title: string;
description: string;
created: string;
updated: string;
size: int;
can_upload: int
}
我如何使用它是在我的 json 请求中输入
let des json =
[json]
|> filter_member "response"
|> flatten
|> to_list;;
let json = Yojson.Basic.from_file "test.json" in
List.iter (fun y -> print_endline(y.aid^"--->"^y.title)) (des json);;
此代码 return 错误类型错误。
(将您的回复记录缩小一点,但仍能说明问题)
type response = {descr:string;
size: int;
can_upload: int}
let record_list =
let open Yojson.Basic.Util in
let f = Yojson.Basic.from_file "test.json" in
List.fold_right
(fun item r_list ->
{descr = member "description" item |> to_string;
size = member "size" item |> to_int;
can_upload = member "can_upload" item |> to_int} :: r_list)
(member "response" f |> to_list)
[]
Yojson 实际上只是 atdgen 的运行时。将类型定义放入 .atd
文件中,atdgen 将使用 yojson:
(* This is file api.atd *) type response_item = { aid: int; thumb_id: string; owner_id: int; title: string; description: string; created: string; updated: string; size: int; can_upload: int } type response = { response: response_item list; }
Atdgen 生成文件 api_t.mli
、api_t.ml
、api_j.mli
和 api_j.ml
,其中 _t
代表类型,_j
代表JSON。请参阅有关如何将 atdgen 与您的构建系统集成的教程。
然后根据您的 OCaml 代码,您所要做的就是:
let record_list = Ag_util.Json.from_file Api_j.read_response "test.json"