Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Couchdb 正在分析OPA中的webclient.Result内容_Couchdb_Opa - Fatal编程技术网

Couchdb 正在分析OPA中的webclient.Result内容

Couchdb 正在分析OPA中的webclient.Result内容,couchdb,opa,Couchdb,Opa,我试图使用webclient模块查询couchDB rest接口(我使用它而不是opa couchDB api,因为我需要获取特定数量的文档) 以下是用于进行查询的代码: listmydocs(dburi)= match WebClient.Get.try_get(dburi) with | { failure = _ } -> print("error\n") | {success=s} -> match WebClient.Result.get_cla

我试图使用webclient模块查询couchDB rest接口(我使用它而不是opa couchDB api,因为我需要获取特定数量的文档)

以下是用于进行查询的代码:

listmydocs(dburi)=
match  WebClient.Get.try_get(dburi) with
      | { failure = _ } -> print("error\n")
      | {success=s} ->  match WebClient.Result.get_class(s) with
          | {success} -> print("{s.content}")                               
          | _         -> print("Error {s.code}")
      end
s.content中给出的结果为以下字符串:

{"total_rows":177,"offset":0,"rows":[
{"id":"87dc6b6d9898eff09b1c8602fb00099b","key":"87dc6b6d9898eff09b1c8602fb00099b","value":{"rev":"1-853bd502e3d80d08340f72386a37f13a"}},
{"id":"87dc6b6d9898eff09b1c8602fb000f17","key":"87dc6b6d9898eff09b1c8602fb000f17","value":{"rev":"1-4cb464c6e1b773b9004ad28505a17543"}}
]}
我想知道解析这个字符串的最佳方法是什么,例如获取ID列表,还是仅获取行字段?
我尝试使用Json.deserialize(s.content),但不确定从哪里开始。

在Opa中,可以使用几种方法来实现两个非序列化Json字符串:

1-第一个是简单地使用Json.deserialize,它接受一个字符串并根据Json规范生成Json AST。 然后,您可以匹配生成的AST以检索所需的信息

match Json.deserialise(a_string) with
| {none} -> /*Error the string doesn't respect json specification*/
| {some = {Record = record}} ->
/* Then if you want 'total_rows' field */
  match List.assoc("total_rows", record) with
  | {some = {Int = rows}} -> rows
  | {none} -> /*Unexpected json value*/
2-另一种方法是使用Json的“神奇”opa反序列化。首先定义与期望值对应的Opa类型。然后使用OpaSerialize.*函数。根据你的例子

type result = {
  total_rows : int;
  offset : int;
  rows : list({id:string; key:string; value:{rev:string}})
}
match Json.deserialize(a_string)
| {none} -> /*Error the string doesn't respect json specification*/
| {some = jsast} ->
  match OpaSerialize.Json.unserialize_unsorted(jsast) with
  | {none} -> /*The type 'result' doesn't match jsast*/
  | {some = (value:result) /*The coercion is important, it give the type information to serialize mechanism*/} ->
    /* Use value as a result record*/
    value.total_rows
谢谢,“神奇”的opa反序列化方法绝对是我想要的。