且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用Java脚本中的双引号嵌套替换字符串

更新时间:2023-12-05 13:12:46

这会将json视为字符串而不是数组.

This treats the json as a string and not an array.

json=json.replace(/"PR_USER8":""/g,'"PR_USER8":0').replace(/"PR_RENT":""/g,'"PR_RENT":0').replace(/"PR_ID":""/g,'"PR_ID":0').replace(/"PR_USUM":""/g,'"PR_USUM":0').replace(/"PR_LEAS":""/g,'"PR_LEAS":0');
return json;```

I think this is better as it treats the json as an object not a string.

    ```
    var jsonString = workflowContext.actions.HTTP_2.outputs.body;
const json =JSON.parse(jsonString);
const ret =[];

const dic = json['Payload'];
const checkFields = ['PR_RENT','PR_ID','PR_USUM','PR_LEAS','PR_USER8'];
for(i in json['Payload'])
{


checkFields.forEach(field=>{
     if(json.Payload[i][field]=="")
    {
        json.Payload[i][field]=0;
       // console.log(field);
    }
}

);
}

return json;
    ```