使用 Groovy 连接 JSON Arrays/Objects
Concatenation of JSON Arrays/Objects using Groovy
如何使用 Java 或 Groovy 合并或连接两个独立的不同 JSON 数组或 JSON 对象并将其视为单个 JSON 对象.
请参阅下面的示例 JSON 我拥有的独立对象
第一个包含职责信息
[
{
"code": "A0001",
"description": "Do strategic planning for long range goals of the university"
},
{
"code": "A0002",
"description": "Administer budgets in excess of 1,000,000"
}]
第二个 JSON 对象包含证书信息
[
{
"code": "CPA",
"description": "Certified Public Accountant"
},
{
"code": "CPR",
"description": "Cardiopulmonary Resuscitation"
},
{
"code": "ELE",
"description": "Electrician's License"
}]
我需要连接并访问以下格式的 JSON `
{
"duties":
[{
"code": "A0001",
"description": "Do strategic planning for long range goals of the university"
},
{
"code": "A0002",
"description": "Administer budgets in excess of 1,000,000"
}],
"Certificates":
[
{
"code": "CPA",
"description": "Certified Public Accountant"
},
{
"code": "CPR",
"description": "Cardiopulmonary Resuscitation"
},
{
"code": "ELE",
"description": "Electrician's License"
}
]
}
请告诉我可用于完成此操作的选项。谢谢
可以做到,例如通过以下方式:
import groovy.json.*
def json1 = """[
{
"code": "A0001",
"description": "Do strategic planning for long range goals of the university"
},
{
"code": "A0002",
"description": "Administer budgets in excess of 1,000,000"
}]"""
def json2 = """[
{
"code": "CPA",
"description": "Certified Public Accountant"
},
{
"code": "CPR",
"description": "Cardiopulmonary Resuscitation"
},
{
"code": "ELE",
"description": "Electrician's License"
}]"""
def duties = new JsonSlurper().parseText(json1)
def certs = new JsonSlurper().parseText(json2)
println JsonOutput.prettyPrint(JsonOutput.toJson ([duties: duties, certificates: certs]))
如何使用 Java 或 Groovy 合并或连接两个独立的不同 JSON 数组或 JSON 对象并将其视为单个 JSON 对象.
请参阅下面的示例 JSON 我拥有的独立对象 第一个包含职责信息
[
{
"code": "A0001",
"description": "Do strategic planning for long range goals of the university"
},
{
"code": "A0002",
"description": "Administer budgets in excess of 1,000,000"
}]
第二个 JSON 对象包含证书信息
[
{
"code": "CPA",
"description": "Certified Public Accountant"
},
{
"code": "CPR",
"description": "Cardiopulmonary Resuscitation"
},
{
"code": "ELE",
"description": "Electrician's License"
}]
我需要连接并访问以下格式的 JSON `
{
"duties":
[{
"code": "A0001",
"description": "Do strategic planning for long range goals of the university"
},
{
"code": "A0002",
"description": "Administer budgets in excess of 1,000,000"
}],
"Certificates":
[
{
"code": "CPA",
"description": "Certified Public Accountant"
},
{
"code": "CPR",
"description": "Cardiopulmonary Resuscitation"
},
{
"code": "ELE",
"description": "Electrician's License"
}
]
}
请告诉我可用于完成此操作的选项。谢谢
可以做到,例如通过以下方式:
import groovy.json.*
def json1 = """[
{
"code": "A0001",
"description": "Do strategic planning for long range goals of the university"
},
{
"code": "A0002",
"description": "Administer budgets in excess of 1,000,000"
}]"""
def json2 = """[
{
"code": "CPA",
"description": "Certified Public Accountant"
},
{
"code": "CPR",
"description": "Cardiopulmonary Resuscitation"
},
{
"code": "ELE",
"description": "Electrician's License"
}]"""
def duties = new JsonSlurper().parseText(json1)
def certs = new JsonSlurper().parseText(json2)
println JsonOutput.prettyPrint(JsonOutput.toJson ([duties: duties, certificates: certs]))