0

我想将 json 数据放在 opa 中的一个对象中。

我看到了一条评论(在 OPA 中解析 webclient.Result 内容),它描述了如何做到这一点,但我尝试了这种方法,但无法使其适用于我的应用程序。

这是我的代码:

type service = {
  string name,
  string kind,
  string vendor 
}
contentResult = WebClient.Get.try_get(url);
match(contentResult)
    {
    case { failure : _ }: "Error, could not connect";   
    case ~{success}:
        match (Json.deserialize(success.content)){          
                case {none}: "Error the string doesn't respect json specification";
            case {some:jsast}: match (OpaSerialize.Json.unserialize_unsorted(jsast)){
                  case {none}: "The type 'result' doesn't match jsast";
                  case {some =(value:service)}: value.name;
                  }                     
            }
    }

我想用 json 文件中的数据创建一个服务类型的对象。

最后一场比赛似乎是个问题,因为它显示在输出中

“结果”类型与 jsast 不匹配

事实上,我的 json 文件中的密钥比上面的 3 个要多,这里是我的日志:

X Data: [
{
    "name": "redis-d80d7",
    "type": "key-value",
    "vendor": "redis",
    "version": "2.2",
    "tier": "free",
    "properties": {},
    "meta": {
        "created": 1310772000,
        "updated": 1310772000,
        "tags": [
            "redis",
            "redis-2.2",
            "key-value",
            "nosql"
        ],
        "version": 1
    }
},
JsAST: {List = [{Record = [{f1 = name; f2 = {String = redis-d80d7}}, 
{f1 = type; f2 = {String = key-value}}, {f1 = vendor; f2 = {String = redis}}, 
{f1 = version; f2 = {String = 2.2}}, {f1 = tier; f2 = {String = free}}, 
{f1 = properties; f2 = {Record = []}}, {f1 = meta; f2 = {Record = 
[{f1 = created; f2 = {Int = 1310772000}}, {f1 = updated; 
f2 = {Int = 1310772000}}, {f1 = tags; f2 = {List = [{String = redis}, 
{String = redis-2.2}, {String = key-value}, {String = nosql}]}}, 
{f1 = version; f2 = {Int = 1}}]}}]}
4

0 回答 0