简体   繁体   English

js_of_ocaml和Deriving_Json

[英]js_of_ocaml and Deriving_Json

I need some help to get js_of_ocaml working. 我需要一些帮助才能让js_of_ocaml正常工作。 There's not much information about it on the net, and the manual is very sparse (no snippets or usage examples, no comment sections). 在网上没有太多关于它的信息,手册非常稀疏(没有片段或用法示例,没有评论部分)。

I have a Card module on the server with a card record. 我在服务器上有一个带有card记录的Card模块。 I'm sending a card list to the client using Ajax, and there I want to read and traverse this list. 我正在使用Ajax向客户端发送card list ,我想阅读并遍历此列表。 What I end up with is this: 我最终得到的是:

let json = Json.unsafe_input (Js.string http_frame.XmlHttpRequest.content) in

...where json has type 'a , according to documentation (not when I run it, of course). ...根据文档(当我运行它时, json类型为'a )。

I can log json##length and get the correct length of the list. 我可以记录json##length并获得正确的列表长度。 Where do I go from here? 我从哪里开始? Ideally, I'd like to use Deriving_Json to type-safe get a card list again, but I could also use a for-loop (not as elegant, but whatever). 理想情况下,我想使用Deriving_Json再次输入类型安全获取card list ,但我也可以使用for循环(不是优雅,但无论如何)。

Type declarations like type name = ... deriving(Json) creates Json_name module. 类型声明,如type name = ... deriving(Json) Json_name type name = ... deriving(Json)创建Json_name模块。 Here is example. 这是一个例子。

type card =  {a: int; b: string; } deriving(Json)
type t = card list deriving(Json)

let _ =
  let str = Json.to_string<t> [{a = 10; b = "abc";}; {a = 20; b = "xyz";}] in
  let rs = Json.from_string<t> str in
  Firebug.console##log(Js.string str);
  List.iter (fun r -> Firebug.console##log_2(r.a, Js.string r.b)) rs

And I'm not sure why, I got "Uncaught ReferenceError: unix_inet_addr_of_string is not defined". 而且我不确定为什么,我得到了“Uncaught ReferenceError:unix_inet_addr_of_string未定义”。

So add function unix_inet_addr_of_string () {return 0;} to js file. 所以将function unix_inet_addr_of_string () {return 0;}到js文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM