使用Groovy将JSON对象数组引入另一个JSON

时间:2016-06-28 16:35:32

标签: json groovy

DealItemCaption

BigDealTicket

由于我对Groovy有点新,我想问你关于使用Groovy的JSON连接的以下问题(请参阅附带的关于问题的图像)。 我在这里有一个JSON输入,我想用Groovy重现:

{"isFinancial":false,
 "isPortfolioTransfer":false,
 "type":"ESS",
 "commodity":"Electricity",
 "timezone":"CET",
 "dealDateTime":1466671066373,
 "dealItems":[{"type":"Energy.StandardShape.FixedPrice",
               "locationType":"grid",
               "dealItemUDFs":[],
               "priceType":"fixed",
               "holidayCalendarId":10001,
               "quantityUnitId":26,
               "priceUnitId":12,
               "dealStart":1483267154000,
               "dealEnd":1514716754000,
               "shapeId":2,
               "balancingGroupId":2,
               "locationId":41,
               "quantity":15000,
               "price":30}],
 "brokerFeeCurrencyId":1,
 "dealTypeId":2,
 "internalEntityId":1399,
 "internalTraderId":305,
 "internalPortfolioId":1,
 "buySell":"-1",
 "externalEntityId":1443,
 "priceRegionId":37,
 "label":"Third Deal Ticket as JIRA Test Candidate",
 "comments":"Third Deal Ticket as JIRA Test Candidate"}

我有使用JSONBuilder生成的内部JSON(在由[]括号分隔的数组中使用的JSON),并且外部JSON通过JSONBuilder生成没有问题,但是,似乎有关于对齐的整体问题的外观问题。给定输出如下:

{"isFinancial":false,
 "isPortfolioTransfer":false,
 "type":"ESS",
 "commodity":"Electricity",
 "timezone":"CET",
 "dealItems":[{"value":"Energy.StandardShape.FixedPrice","key":"type"},
              {"value":"grid","key":"locationType"},
              {"value":[],"key":"dealItemUDFs"},
              {"value":"fixed","key":"priceType"},
              {"value":10001,"key":"holidayCalendarId"},
              {"value":26,"key":"quantityUnitId"},
              {"value":12,"key":"priceUnitId"},
              {"value":1483225200000,"key":"dealStart"},
              {"value":1514674800000,"key":"dealEnd"},
              {"value":2,"key":"shapeId"},
              {"value":1,"key":"balancingGroupId"},
              {"value":42,"key":"locationId"},
              {"value":15000,"key":"quantity"},
              {"value":30,"key":"price"}],
 "dealDateTime":1467116296197,
 "brokerFeeCurrencyId":6,
 "dealTypeId":2,
 "internalEntityId":1399,
 "internalTraderId":306,
 "internalPortfolioId":1,
 "buySellId":"-1",
 "externalEntityId":1443,
 "priceRegionId":37,
 "label":"Latest JIRA Test Case Candidate",
 "comments":"Latest JIRA Test Case Candidate"}

而不是dealItems的值键对,应该有以下内容 JSON

[{"type":"Energy.StandardShape.FixedPrice",
 "locationType":"grid",
 "dealItemUDFs":\[\],
 "priceType":"fixed",
 "holidayCalendarId":10001,
 "quantityUnitId":26,
 "priceUnitId":12,
 "dealStart":1483225200000,
 "dealEnd":1514674800000,
 "shapeId":2,
 "balancingGroupId":1,
 "locationId":42,
 "quantity":15000,
 "price":30}][1]

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用grails.converters.JSON

以下内容应该完全符合您的要求:

grails.converters.JSON.parse("""
    {"isFinancial":false,
    "isPortfolioTransfer":false,
    "type":"ESS",
    "commodity":"Electricity",
    "timezone":"CET",
    "dealDateTime":1466671066373,
    "dealItems":[{"type":"Energy.StandardShape.FixedPrice",
                 "locationType":"grid",
                 "dealItemUDFs":[],
                 "priceType":"fixed",
                 "holidayCalendarId":10001,
                 "quantityUnitId":26,
                 "priceUnitId":12,
                 "dealStart":1483267154000,
                 "dealEnd":1514716754000,
                 "shapeId":2,
                 "balancingGroupId":2,
                 "locationId":41,
                 "quantity":15000,
                 "price":30}],
    "brokerFeeCurrencyId":1,
    "dealTypeId":2,
    "internalEntityId":1399,
    "internalTraderId":305,
    "internalPortfolioId":1,
    "buySell":"-1",
    "externalEntityId":1443,
    "priceRegionId":37,
    "label":"Third Deal Ticket as JIRA Test Candidate",
    "comments":"Third Deal Ticket as JIRA Test Candidate"}
""")

希望有所帮助!