首先用JavaScript字符串方法編輯字符串。
function processString() {
var originalStr = "(OpportunityProduct1:{Product10Id=01726388aaa, Quantity=4.00, ListPrice=3.00, Sales_Price__A=3.00})";
var i=0, thisCharacter = "";
//Remove spaces
for (i=0;i<originalStr.length;i+=1) {
thisCharacter = originalStr[i];
//Logger.log(thisCharacter)
if (thisCharacter === " ") {
Logger.log(i);
var front = originalStr.slice(0,i);
var back = originalStr.slice(i+1);
originalStr = front + back;
};
};
Logger.log(originalStr)
var strNewObject = "";
var myRegEx = new RegExp("=","g");
strNewObject = originalStr.replace(myRegEx, '":"');
strNewObject = strNewObject.replace("{", '{"');
strNewObject = strNewObject.replace("}", '"}');
strNewObject = strNewObject.replace(/,/g, '","');
strNewObject = strNewObject.replace("(", '{"'); //Need out curly brace for valid JSON
strNewObject = strNewObject.replace(")", "}"); //Need out curly brace for valid JSON
strNewObject = strNewObject.replace(":", '":'); //
Logger.log('strNewObject: ' + strNewObject);
var newObject = JSON.parse(strNewObject);
Logger.log(newObject);
Logger.log(typeof newObject);
Logger.log(newObject.OpportunityProduct1.Quantity)
};
您可以將字符串轉換回對象與JSON
服務:
var myNewObject = JSON.parse(productVar);
使用快捷鍵「Ctrl」鍵+「空格鍵」,在Google Apps腳本編輯器,彈出的列表可用的服務。
請問應用腳本的JSON解析器是否接受這種語法? Salesforce正在返回「Name = Value」而不是「Name」:Value'。可能需要做一些字符串操作(或者寫一個reviver函數) – jjjjoe
不是的,Salesforce的字符串不是JSON字符串,它只是「一個字符串」。我們真的被卡住了。 –
查看更新的答案。 –