当我尝试使用以下代码解析google appscript中的json时,我希望将dataAll作为对象,但调试器将dataAll显示为字符串,因此我无法解析json。
var response = UrlFetchApp.fetch(aUrl, getUrlFetchOptions());
var resposnsestring = response.getContentText();
var dataAll = JSON.parse(response.getContentText());
Here is the data/
{
"DataSet" : [ {
"Record" : [ {
"attributes" : {
"type" : "Opportunity",
"url" : "/services/data/v31.0/sobjects/Opportunity/006i000000GcJUfAAN"
},
"Name" : "EM- Deadul Technology",
"Amount" : 100.00,
"AccountId" : "001i000000Q9c8vAAB",
"Account" : {
"attributes" : {
"type" : "Account",
"url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
},
"Name" : "Ellie Mae",
"Id" : "001i000000Q9c8vAAB"
},
"Id" : "006i000000GcJUfAAN"
}, {
"attributes" : {
"type" : "Opportunity",
"url" : "/services/data/v31.0/sobjects/Opportunity/006i000000GcJUDAA3"
},
"Name" : "EM- Deadul Technology",
"Amount" : 100.00,
"AccountId" : "001i000000Q9c8vAAB",
"Account" : {
"attributes" : {
"type" : "Account",
"url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
},
"Name" : "Ellie Mae",
"Id" : "001i000000Q9c8vAAB"
},
"Id" : "006i000000GcJUDAA3"
}, {
"attributes" : {
"type" : "Opportunity",
"url" : "/services/data/v31.0/sobjects/Opportunity/006i000000GcJUEAA3"
},
"Name" : "EM- Deadul Technology",
"Amount" : 100.00,
"AccountId" : "001i000000Q9c8vAAB",
"Account" : {
"attributes" : {
"type" : "Account",
"url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
},
"Name" : "Ellie Mae",
"Id" : "001i000000Q9c8vAAB"
},
"Id" : "006i000000GcJUEAA3"
}, {
"attributes" : {
"type" : "Opportunity",
"url" : "/services/data/v31.0/sobjects/Opportunity/006i000000Lpbl0AAB"
},
"Name" : "EM- Deadul Technology",
"Amount" : 100.00,
"AccountId" : "001i000000Q9c8vAAB",
"Account" : {
"attributes" : {
"type" : "Account",
"url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
},
"Name" : "Ellie Mae",
"Id" : "001i000000Q9c8vAAB"
},
"Id" : "006i000000Lpbl0AAB"
}, {
"attributes" : {
"type" : "Opportunity",
"url" : "/services/data/v31.0/sobjects/Opportunity/006i000000Lpbl1AAB"
},
"Name" : "EM- Deadul Technology",
"Amount" : 100.00,
"AccountId" : "001i000000Q9c8vAAB",
"Account" : {
"attributes" : {
"type" : "Account",
"url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
},
"Name" : "Ellie Mae",
"Id" : "001i000000Q9c8vAAB"
},
"Id" : "006i000000Lpbl1AAB"
}, {
"attributes" : {
"type" : "Opportunity",
"url" : "/services/data/v31.0/sobjects/Opportunity/006i000000Lpbl2AAB"
},
"Name" : "EM- Deadul Technology",
"Amount" : 100.00,
"AccountId" : "001i000000Q9c8vAAB",
"Account" : {
"attributes" : {
"type" : "Account",
"url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
},
"Name" : "Ellie Mae",
"Id" : "001i000000Q9c8vAAB"
},
"Id" : "006i000000Lpbl2AAB"
}, {
"attributes" : {
"type" : "Opportunity",
"url" : "/services/data/v31.0/sobjects/Opportunity/006i000000Lpbl3AAB"
},
"Name" : "EM- Deadul Technology",
"Amount" : 100.00,
"AccountId" : "001i000000Q9c8vAAB",
"Account" : {
"attributes" : {
"type" : "Account",
"url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
},
"Name" : "Ellie Mae",
"Id" : "001i000000Q9c8vAAB"
},
"Id" : "006i000000Lpbl3AAB"
}, {
"attributes" : {
"type" : "Opportunity",
"url" : "/services/data/v31.0/sobjects/Opportunity/006i000000A89SCAAZ"
},
"Name" : "EM- Deadul Technology",
"Amount" : 100.00,
"AccountId" : "001i000000Q9c8vAAB",
"Account" : {
"attributes" : {
"type" : "Account",
"url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
},
"Name" : "Ellie Mae",
"Id" : "001i000000Q9c8vAAB"
},
"Id" : "006i000000A89SCAAZ"
} ],
"ObjectName" : "Opportunity",
"DataSetName" : "opp"
}, {
"Record" : [ {
"attributes" : {
"type" : "Account",
"url" : "/services/data/v31.0/sobjects/Account/001i000000Q9c8vAAB"
},
"Name" : "Ellie Mae",
"Phone" : "(671) 214-5678",
"Fax" : "(671) 214-5600",
"Id" : "001i000000Q9c8vAAB"
} ],
"ObjectName" : "Account",
"DataSetName" : "acc"
} ]
}
答案 0 :(得分:1)
使用此:
var dataAll = JSON.parse(resposnsestring);
如果JSON.parse()
没有返回一个对象,那么字符串的构造方式有问题,它不是JSON格式。
答案 1 :(得分:1)
我从源接收的json数据有转义字符,当我调用json.parse时,它返回一个没有转义字符串的字符串。我在返回对象的新字符串上再次使用了json.parse