首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将内存节点转换为JSON

将内存节点转换为JSON
EN

Stack Overflow用户
提问于 2021-12-18 22:42:46
回答 2查看 489关注 0票数 2

我需要将der内存节点转换为JSON。预期产出:

代码语言:javascript
复制
{
    "citations": [
        {
            "cited": "classes",
            "procceding": [
                "applied",
                "considered",
                "followed"
            ]
        },
        {
            "cited": "toCase",
            "procceding": [
                "Ty Corp Ltd v Nu Inc",
                "PY Arbitrage v Bank of WN"
            ]
        }
    ]
}

我的XQuery:

代码语言:javascript
复制
let $nodes :=
<cases>
  <citations>
    <classes>
      <text>applied</text>
      <text>considered</text>
      <text>followed</text>
    </classes>
    <toCase>
      <text>Ty Corp Ltd v Nu Inc</text>
      <text>PY Arbitrage v Bank of WN</text>
    </toCase>
  </citations>
</cases>
return     
let $map := map:map()
let $p :=
    for $node in $nodes/citations/node()
    let $nodeName := local-name($node)
    let $c :=
      map:put($map, "citations", map:new(map:entry('cited', map:new(map:entry($nodeName, (for $value in $node/node() return $value/fn:string(.)) ))) )  )
    return $c
return xdmp:to-json($map)

$nodes被错误地转化为:

{“引证”:{“引证”:{"toCase":"Ty Corp有限公司诉Nu公司“,"PY仲裁案诉WN银行”}

如何获得预期的JSON?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-12-23 02:28:30

-)您可以使用json: Object ()来构建内存中的JSON模型(相当于XML模式),并保持对象的顺序。我重构并精简了我的xml模块之一,如下所示。它应该能满足你的需要。

代码语言:javascript
复制
declare function local:marshal-json(
  $master-node as node()
) as item()
{
  let $master-object := json:object()
  let $_ :=
    for $child-node in $master-node/node()
    let $child-name := local-name($child-node)
    return
      map:put($master-object, $child-name,
          for $floor in $child-node/node() return local:floor-object($floor)
      )      
  return $master-object        
};

declare function local:floor-object(
  $child-node as node()  
) as item()
{
  let $floor-object := json:object()
  let $_ := map:put($floor-object, "cited", local-name($child-node))
  let $floor-values := json:array()
  let $_ :=
    for $gc in $child-node/node()
    return json:array-push($floor-values, $gc/fn:string(.))
  let $_ := map:put($floor-object, "procceding", $floor-values)  
  return
    $floor-object  
};

let $nodes :=
<cases>
  <citations>
    <classes>
      <text>applied</text>
      <text>considered</text>
      <text>followed</text>
    </classes>
    <toCase>
      <text>Ty Corp Ltd v Nu Inc</text>
      <text>PY Arbitrage v Bank of WN</text>
    </toCase>
  </citations>
</cases>
return 
  local:marshal-json($nodes)

-)我不能不赞成你的设计。但我赞同这种精神。您应该能够使用类似的逻辑将XML转换为JSON (参数不是硬编码的JSON名称)。

示例:转换

代码语言:javascript
复制
<investment>
    <entity>
        <name>XYZ</name>
    </entity>
    <bogus>encrypted</bogus>
    <property>
        <Canadian>
            <propertyType>eligible dividend of CPC</propertyType>
            <notionalAmount>10000</notionalAmount>
        </Canadian>
        <foreign>
            <propertyType>dividend of trust</propertyType>
            <notionalAmount>7000</notionalAmount>
            <foreignWithholding>660</foreignWithholding>
        </foreign>
    </property>
    <capital>
        <portfolio>
            <name>HF25</name>
            <inadequateConsiderationFMV>10000</inadequateConsiderationFMV>
            <transferredAmount>15000</transferredAmount>
            <gainLossOnDisposition>11000</gainLossOnDisposition>
        </portfolio>
        <portfolio>
            <name>UL1</name>
            <superficialLoss>17000</superficialLoss>
            <reacquired>5000</reacquired>
            <gainLossOnDisposition>-2000</gainLossOnDisposition>
        </portfolio>
        <security-CUSIP1>
            <gainLossOnDisposition>-3600</gainLossOnDisposition>
        </security-CUSIP1>
    </capital>
</investment>

转到JSON:

代码语言:javascript
复制
{
    "entity": {
        "name": "XYZ"
    },
    "bogus": null,
    "property": {
        "Canadian": {
            "propertyType": "eligible dividend of CPC",
            "notionalAmount": 10000
       },
        "foreign": {
            "propertyType": "dividend of trust",
            "notionalAmount": 7000,
            "foreignWithholding": 660
        }
    },
    "capital": {
        "portfolio": [
            {
                "name": "HF25",
                "inadequateConsiderationFMV": 10000,
                "transferredAmount": 15000,
                "gainLossOnDisposition": 11000
            },
            {
                "name": "UL1",
                "superficialLoss": 17000,
                "reacquired": 5000,
                "gainLossOnDisposition": -2000
            }
        ], 
        "security-CUSIP1": {
            "gainLossOnDisposition": -3600
        }
    }
}

我使用低级别的API具有更多的优势:它根据指令将XML /text转换为JSON stringnumber;它符合自定义的JSON模型、对象和数组结构;它遵守金融领域的XML/JSON命名约定。

--也就是说,这个编程模型是内存密集型的。如果XSLT3在流模型中解析和转换JSON,那么它的内存效率会更高。我从来没有像现在一样使用XSLT3 JSON解析器,也不能评论它的有效性。

票数 1
EN

Stack Overflow用户

发布于 2021-12-18 23:14:09

代码语言:javascript
复制
let $nodes :=
  <cases>
    <citations>
      <classes>
        <text>applied</text>
        <text>considered</text>
        <text>followed</text>
      </classes>
      <toCase>
        <text>Ty Corp Ltd v Nu Inc</text>
        <text>PY Arbitrage v Bank of WN</text>
      </toCase>
    </citations>
  </cases>
return     

let $citations := 
  for $node in $nodes/citations/node()
  let $nodeName := local-name($node)
  return 
    map:new(( 
      map:entry('cited', $nodeName), 
      map:entry("procceding", for $value in $node/node() return $value/fn:string(.) )
    ))   
return 
  map:entry("citations", $citations) => xdmp:to-json() 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70407708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档