代码之家  ›  专栏  ›  技术社区  ›  R_SS

用于json键数组的groovy jsonBuilder

  •  0
  • R_SS  · 技术社区  · 2 年前

    应为json,如下所示

    我有一个位置列表和立方体列表

    {
        "Company": "my company",
        "Locations": {
            "India": {
                "cubicals": [
                    "A-8954","B-3212"
                ]
            },
            "USA": {
                "cubicals": [
                    "A-8954","B-3212"
                ]
            }
        }
    }
    

    编写groovy代码如下,cubicalList和nationsList的维度相同

    cubicalList=["A-8954","B-3212"]
    nationsList=["India", "USA"]
    def json = new groovy.json.JsonBuilder()
    name="my company"
    json {
        company name
        Locations {
                "${nationsList}" (
                {
                    cubicals cubicalsList
                }
                )
        }
    }
    
    println json.toPrettyString()
    

    在这里,印度和美国走到一起,对此的任何投入都将非常有用

    {
        "company": "my company",
        "Locations": {
            "[India, USA]": {
                "cubicals": [
                    "A-8954",
                    "B-3212"
                ]
            }
        }
    }
    
    1 回复  |  直到 2 年前
        1
  •  0
  •   tim_yates    2 年前

    你的意思是

    json {
        company name
        Locations {
            nationsList.each { nation ->
                "${nation}" {
                    cubicals cubicalsList
                }
            }
        }
    }
    

    ?