2013-07-24 36 views
6

我需要一些幫助才能讓js_of_ocaml工作。在網上沒有太多關於它的信息,而且手冊非常稀少(沒有片段或使用示例,也沒有評論部分)。js_of_ocaml和Deriving_Json

我有一個Card模塊在服務器上card記錄。我使用Ajax向客戶端發送card list,在那裏我想閱讀並遍歷此列表。我結束了是這樣的:

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

...其中json的類型爲'a,根據文件(不是當我運行它,當然)。

我可以登錄json##length並獲得正確的列表長度。我從哪裏出發?理想情況下,我想用Deriving_Json類型安全再次獲得card list,但我也可以使用for循環(不優雅,但不管)。

回答

3

類型聲明,如type name = ... deriving(Json)創建Json_name模塊。 這裏是例子。

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 

而且我不知道爲什麼,我得到了「未捕獲的ReferenceError:unix_inet_addr_of_string沒有定義」。

因此,將function unix_inet_addr_of_string() {return 0;}添加到js文件。