介绍对象到使用Groovy
问题描述:
因为我是一个有点新的Groovy中,我想请教您有关与使用Groovy JSON串联以下问题的另一个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"}
我有内部JSON(由[]括号中限定的阵列内使用的一个)中产生用JSONBuilder并且还没有通过JSONBuilder问题产生外JSON,然而,似乎是对于给定输出的allignment化妆品的问题如下:
{"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]
答
您是否尝试过使用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"}
""")
希望帮助!
非常感谢你吉姆 - 我会尝试这个解决方案。顺便再提一个问题:这个图书馆在哪里?我在这里指的是将其导入到SOAP UI的可能性,当然 –
所以你的意思是说,基本上如果我想我的大JSON中的DealItems键下的值具有相同的结果,如第二个摘录中所发布的,我只需要使用这个库? (对不起,如果我不清楚上一个问题,我的意思是把第二部分的JSON作为一个数组元素,而不是显示键值对的JSON - 两个JSON在单独使用时都能正确生成,问题与他们的加入)。感谢您的理解 –
道歉,我提到的插件是一个grails插件,可在此处找到:https://grails.org/wiki/Converters%20Plugin。 –