Как избежать строки Json в groovy?

#json #groovy #escaping

Вопрос:

У меня есть этот набор входных данных Json

 {
        "P_Key": "be087d39271444e2971a144997c1491a",
        "RecordRef": "715027",
        "StepTypeRef": "28",
        "DateTo": "2017-04-01 00:00:00.000",
        "DateForm": "2017-04-01 00:00:00.000",
        "Distribution_Channel": " ",
        "PlantId": " ",
        "DistributorId": " ",
        "ProductId": "85153180",
        "PriceClassID": 0,
        "MinQuantityUom": "PK",
        "ValueUom": "MYR",
        "Value": 30.6,
        "DeleteFlag": 0,
        "BottlerID__c": "QSRT"
      }
 

Я хочу преобразовать в этот вывод (мой ожидаемый вывод)

 "[{"P_Key":"001C3D60BEasdaq661w27084236D896BB7774123ElB","RecordRef":"12453209601000103712793","DiscountSchemeLineID":"1","StepTypeRef":"28","MarketScopeCode":"1","CD_TradeChannel":"","CD_DistributorId":"HMK6","CD_CustomerId":"","CD_Salesrep":"","CD_PriceClassId":"","CD_SalesGroup":"","CD_PlantId":"HMK6","PD_ProductId":"00700000011103793","PD_PlantId":"HMK6","PD_IPC":"","PD_DistributorId":"HMK6","PD_MRP":"","PD_DistributionChannel":"","MonitoringScopeCode":"","DiscountChannelOnlyTo":"","DateFrom":"2018-02-01 00:00:00.000","DateTo":"2020-02-01 00:00:00.000","Min":"0.00","Max":"999.00","MinQuantityUom":"CS","CalcType":"","ValueUom":"TRY","Value":"10.00","ValuePerQuantity":"1","FreeGoodFreeProductRef":"","FreeGoodUOM":"","GroupRef":"999.00","ScaleDataFlag":"true","DeleteFlag":"true","BottlersCode":"RIT","Tax":""}]"
 

Я попробовал использовать строку newDataSetJson = dataset.toString();

 import com.oracle.e1.common.OrchestrationAttributes;
import java.text.SimpleDateFormat;
import groovy.json.*;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import groovy.json.JsonBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;
import groovy.json.StringEscapeUtils;


HashMap<String, Object> main(OrchestrationAttributes orchAttr, HashMap inputMap)
{
  HashMap<String, Object> returnMap = new HashMap<String, Object>();
   //Input is the full output of the data request
    String dataSetIn = (String)inputMap.get("DataSetIn");
    
    //create an object out of it
    def jsonSlurper = new JsonSlurper();    
    def object = jsonSlurper.parseText(dataSetIn);
    
   // get the record set   
    def dataset = object.GridData;
    def newDataSet = [];    

    //create a json string from the new data set
    String newDataSetJson = dataset.toString();
    
   
    //set that to the array variable defined in the custom
    returnMap.put("GridData",newDataSetJson);

  return returnMap;
}
 

но это производит (не мой ожидаемый результат)

 "{[{P_Key=14c54f37f6734b8ea78be6b69a8980a2, RecordRef=91331, StepTypeRef=28, DateTo=2016-12-01 00:00:00.000, DateForm=2016-12-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=53001300, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=115, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=515a5159b7b24f9f9a9beb990124baeb, RecordRef=91332, StepTypeRef=28, DateTo=2016-12-19 00:00:00.000, DateForm=2016-12-19 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=53001300, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=1E 2, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=ba6964b2b4a5420eb5d7f8e2976fa829, RecordRef=91333, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=53001300, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=1E 2, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=7a2eaf6b05234afeb0c8934d6073aa6b, RecordRef=91334, StepTypeRef=28, DateTo=2016-12-01 00:00:00.000, DateForm=2016-12-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=53003300, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=115, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=d56d86035ed34ec6812316fb584e6690, RecordRef=91335, StepTypeRef=28, DateTo=2016-12-19 00:00:00.000, DateForm=2016-12-19 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=53003300, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=1E 2, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=21394a513b754e04871442975e569b0a, RecordRef=91336, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=53003300, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=1E 2, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=6c8e048fa4d345e7a4a10fd0df364d31, RecordRef=91337, StepTypeRef=28, DateTo=2016-12-01 00:00:00.000, DateForm=2016-12-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=53004300, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=112, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=2abf4e45095548bdb004c9570e4490f6, RecordRef=91338, StepTypeRef=28, DateTo=2016-12-19 00:00:00.000, DateForm=2016-12-19 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=53004300, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=1E 2, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=04e726f31c434623b6369455212813d4, RecordRef=91339, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=53004300, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=1E 2, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=cf649775702b4af5accb8e916cec3047, RecordRef=913310, StepTypeRef=28, DateTo=2016-12-01 00:00:00.000, DateForm=2016-12-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=SH, PriceClassID=0, MinQuantityUom=PK, ValueUom=MYR, Value=42, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=1f3da7f7e028434f9e1c1989f5e65977, RecordRef=913311, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=SH, PriceClassID=0, MinQuantityUom=PK, ValueUom=MYR, Value=42, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=98f1e93fd6fc4e50a40e12ffac7cdb6e, RecordRef=913312, StepTypeRef=28, DateTo=2016-12-01 00:00:00.000, DateForm=2016-12-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=50018301, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=170.6, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=072b04e6e26f49d19563b316027a11fc, RecordRef=913313, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=50018301, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=170.6, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=08a3f71b952c483291736dbd6a1d4e33, RecordRef=913314, StepTypeRef=28, DateTo=2016-12-01 00:00:00.000, DateForm=2016-12-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=60003301, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=109.62, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=29f6b2d9dc5e406e947020006e9be252, RecordRef=913315, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=60003301, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=109.62, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=13d3c5dedb994941b51e4eeba2fbde1d, RecordRef=913316, StepTypeRef=28, DateTo=2016-12-01 00:00:00.000, DateForm=2016-12-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=62011301, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=142.24, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=eaf3de3f206a40e6b224acf74d0851fc, RecordRef=913317, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=62011301, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=134.8, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=e8d0ef07e1cd41dea08f2ef21065573d, RecordRef=913318, StepTypeRef=28, DateTo=2016-12-01 00:00:00.000, DateForm=2016-12-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=64023301, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=134.8, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=af43f4bbbaa74e639cae11c9848304a7, RecordRef=913319, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=64023301, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=134.8, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=094f3f9efa3a419d8c81c1d2f31f62c8, RecordRef=913320, StepTypeRef=28, DateTo=2016-12-01 00:00:00.000, DateForm=2016-12-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=62026301, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=108.6, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=9eb012de00124f2da5c344fcc12c9c69, RecordRef=913321, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=62026301, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=108.6, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=29f21fb3ad5c41b78dfa52558e3cfc2e, RecordRef=913322, StepTypeRef=28, DateTo=2016-12-01 00:00:00.000, DateForm=2016-12-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=52003301, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=92, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=626f0445ed4b44c7b64bfecaf79cc6db, RecordRef=913323, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=52003301, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=92, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=c3751c4cdf5b47138c8e459a063393c0, RecordRef=913324, StepTypeRef=28, DateTo=2016-12-01 00:00:00.000, DateForm=2016-12-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=80009380, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=6E 1, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=6a4632a240c64e8f84169d306132975c, RecordRef=913325, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=80009380, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=6E 1, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=2d877402d95c4351a340788635ee0177, RecordRef=913326, StepTypeRef=28, DateTo=2016-12-01 00:00:00.000, DateForm=2016-12-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=85153180, PriceClassID=0, MinQuantityUom=PK, ValueUom=MYR, Value=30.6, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=2a046c0856b04371b5f64800bc7288b6, RecordRef=913327, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=85153180, PriceClassID=0, MinQuantityUom=PK, ValueUom=MYR, Value=30.6, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=22e32fc908c44d839cbcccfa2890b0c9, RecordRef=913328, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=56007300, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=85, DeleteFlag=0, BottlerID__c=QSRT}, {P_Key=3c97185ae2d8448d9101e8a51145805e, RecordRef=913329, StepTypeRef=28, DateTo=2017-04-01 00:00:00.000, DateForm=2017-04-01 00:00:00.000, Distribution_Channel= , PlantId= , DistributorId= , ProductId=50007300, PriceClassID=0, MinQuantityUom=CR, ValueUom=MYR, Value=108, DeleteFlag=0, BottlerID__c=QSRT}]}"
  }
 

Ответ №1:

.toString() не генерирует JSON — это общий отладочный вывод и непригоден для любого способа сериализации.

Самый простой способ сериализации простых типов данных в формате JSON JsonOutput -это, например. groovy.json.JsonOutput.toJson(dataset)

Другие (уже импортированные версии) тоже в порядке (Джексон, JsonBuilder).

Комментарии:

1. Спасибо. Но как убежать от них к этому «[{«P_Key»:»001C3D60BEasdaq661w27084236D896BB7774123ElB»,»RecordRef»:»12453209601000103712793″}] ?

2. Это не «побег» — это просто вывод строки, содержащей JSON. Если вы хотите, чтобы строки, закодированные в JSON, находились в другой структуре данных, которая кодируется в JSON, сделайте это. Например. JsonOutput.toJson([data: JsonOutput.toJson(dataset)])