如何在groovy中转义Json字符串?

问题描述 投票:0回答:2

我有这个 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}]}"
  }
json groovy escaping
2个回答
1
投票

.toString()
不生成 JSON - 它是通用调试输出并且 不适合任何序列化方式。

将简单数据类型序列化为 JSON 的最简单方法是

JsonOutput
。例如。
groovy.json.JsonOutput.toJson(dataset)

其他(已经导入的版本)也很好(杰克逊, JsonBuilder)。


0
投票

您可以按如下方式设置“转义”变量的值。

${__groovy(groovy.json.StringEscapeUtils.escapeJavaScript(vars.get("raw")),)}

例如,这是用户定义变量配置元素下的变量定义。

      <elementProp name="escaped" elementType="Argument">
        <stringProp name="Argument.name">escaped</stringProp>
        <stringProp name="Argument.value">${__groovy(groovy.json.StringEscapeUtils.escapeJavaScript(vars.get(&quot;raw&quot;)),)}</stringProp>
        <stringProp name="Argument.metadata">=</stringProp>
      </elementProp>

Groovy 文档

© www.soinside.com 2019 - 2024. All rights reserved.