gpt4 book ai didi

java - 如何使用 Bolt 使用 Json 进行转换?

转载 作者:行者123 更新时间:2023-12-02 10:17:05 25 4
gpt4 key购买 nike

我必须将一个 JSON 转换为另一个 JSON, 我是 jolt 的新手。如果您在 java 中有任何其他方法,请告诉我。输入可以以嵌套方式具有许多其他属性。我必须编写通用代码,它可以使用 JSON 中的所有字段并将其转换为我提到的所需输出。

输入

  {   
"id": "123456789",
"OrderType": "ABC",
"Abc": [
{
"Name": "Pluto",
"Value": "Charon"
},
{
"Name": "Earth",
"Value": "Moon"
}
]
}

期望的输出

  "MyFieldList": [
{
"Footer": "My Footer",
"fieldList": [
{
"label": "id",
"fieldName": "id",
"fieldValue": "123456789",
"editable": false,
"dataType": "STRING"
},
{
"label": "OrderType",
"fieldName": "OrderType",
"fieldValue": "ABC",
"editable": false,
"dataType": "STRING"
},
{
"label": "Pluto",
"fieldName": "Pluto",
"fieldValue": "Charon",
"editable": false,
"dataType": "STRING"
},
{
"label": "Earth",
"fieldName": "Earth",
"fieldValue": "Moon",
"editable": false,
"dataType": "STRING"
}]
}
]

我尝试过使用这个震动规范,但是,我无法弄清楚嵌套部分如何压平它。

 {
"operation": "shift",
"spec": {
"*": {
"$": "[#2].fieldName",
"@": "[#2].fieldValue",
"#false": "[#2].editable",
"# ": "[#2].Size",
"#STRING": "[#2].dataType"
}
}
}

最佳答案

重要的部分是在变成 fieldList 之前创建一个数组数组:

[
{
"operation": "shift",
"spec": {
"id": {
"$": "[#1].[#1].fieldName",
"@": "[#1].[#1].fieldValue",
"#false": "[#1].[#1].editable",
"#STRING": "[#1].[#1].dataType"
},
"OrderType": {
"$": "[#2].[#1].fieldName",
"@": "[#2].[#1].fieldValue",
"#false": "[#2].[#1].editable",
"#STRING": "[#2].[#1].dataType"
},
"Abc": {
"*": {
"Name": "[#3].[&1].fieldName",
"Value": "[#3].[&1].fieldValue",
"#false": "[#3].[&1].editable",
"#STRING": "[#3].[&1].dataType"
}
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "MyFieldList.fieldList.[]"
}
}
},
{
"operation": "default",
"spec": {
"MyFieldList": {
"Footer": "My Footer"
}
}
}
]

关于java - 如何使用 Bolt 使用 Json 进行转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54612809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com