由JSON.stringify创建的字符串在由JSON.parse评估时会产生错误
我将通过JSON.stringify创建的字符串作为参数传递给另一个通过JSON.parse处理它的函数,但解析函数正在生成一个错误。
被串化的JSON是通过Service_JSON(PHP梨包)从Xero API(会计包)创建的JSON对象的一部分,它验证了正确性。
JSON.parse似乎错误的任何'字符。
问题1:stringify不能像新行和单引号那样转义特殊字符吗?
问题2:我不应该期望JSON.stringifyed字符串生成没有错误将在JSON.parse中解析没有错误,因为它们是互相恭维/相反的功能?
编辑:
Heaviliy编写了如下代码:
function PANDA_INVOICING_JS_save_invoice_add(theWindowID,loadPopupFrom) {
var theForm = $('#'+theWindowID+'_form');
var theFrameID = Date.parse(new Date());
$.ajax({
url: '?module=invoicing&action=save_invoices_add&window_uid='+theWindowID,
method: 'post',
data: theForm.serialize(),
}).done(function(result) {
var theJSON = result;
if (loadPopupFrom) {
var theURL = '?module=invoicing&action=display_invoices_details&invoice_id='+theInvoiceID+'&loadfrom='+loadPopupFrom+'&window_uid='+theFrameID;
} else {
var theURL = '?module=invoicing&action=display_invoices_details&invoice_id='+theInvoiceID;
}
/**
* XERO Allocate Credit
*/
var loadfunctions = false;
if (APP_JS_check_nestedobjects(theJSON,'data','xero_data','response')) {
if ($.isNumeric(theJSON.data.xero_data.credits.availablecredit) && theJSON.data.xero_data.credits.availablecredit > 0) {
var theXEROdata = JSON.stringify(theJSON.data.xero_data);
var loadfunctions = [
{
functionname: 'PANDA_PLUGIN_JS_xero_credit_notify',
'arguments': '####__WINDOWID__####,''+theXEROdata+'',''+theURL+'''
}
];
}
}
/* Load the new invoice */
APP_JS_frame((loadPopupFrom) ? true : false, theURL,loadfunctions);
}
});
/**
* Return true
*/
return true;
}
在APP_JS_frame中传递此代码(是的:在这种情况下,eval是一个邪恶但必要的,并且没有用户数据被评估):
if (typeof(loadfunctions)=='object') {
for (i=0;i<loadfunctions.length;i++) {
if (! loadfunctions[i].functionname) continue; //if functionname is not defined move on
if (loadfunctions[i].arguments) {
arguments = $PHP_JS.str_replace('####__WINDOWID__####',theFrameID,loadfunctions[i].arguments)
} else {
arguments = '';
}
eval(loadfunctions[i].functionname + '('+arguments+')');
}
}
然后通过这个函数实际抛出错误:
function PANDA_PLUGIN_JS_xero_credit_notify(theFrameID, theXerodata, theURL) {
/* Parse JSON and set variables */
theXerodata = theXerodata.escapeJSON();
theXerodata = JSON.parse(theXerodata);
}
escapeJSON()如下所示:
String.prototype.escapeJSON = function() {
return this.replace(/[]/g, '\')
.replace(/[b]/g, 'b')
.replace(/[f]/g, 'f')
.replace(/[n]/g, 'n')
.replace(/[r]/g, 'r')
.replace(/[t]/g, 't')
.replace(/[']/g, "'");
};
至于实际的xeroData字符串 - 它应该很重要 - 因为JSON.stringify应该为JSON.parse生成有效的JSON - 对吧? 但为了数据的缘故,这里是JSON:
{
"xero_data": {
"invoices": {
"invoice_id": "4323",
"invoice_balance": "123.00",
"date_invoice": "2016-11-20",
"contact_id": "2558",
"contact_name": "Person, Test"
},
"response": {
"return_code": 200,
"xero_response": {
"Id": "dedb737c-ce42-433f-aa2b-02478b839bbf",
"Status": "OK",
"ProviderName": "PANDA-Developer",
"DateTimeUTC": "2016-11-19T23:28:28.6858694Z",
"Invoices": {
"Invoice": {
"Contact": {
"ContactID": "b1e95873-81b2-44f6-b46a-3d4df44ef602",
"ContactStatus": "ACTIVE",
"Name": "Test Person",
"FirstName": "Test",
"LastName": "Person",
"EmailAddress": "emailaddress",
"Addresses": {
"Address": [{
"AddressType": "STREET"
}, {
"AddressType": "POBOX",
"AddressLine1": "P.O. Box",
"City": "Town",
"Region": "Otago",
"PostalCode": "1201",
"Country": "New Zealand"
}]
},
"Phones": {
"Phone": [{
"PhoneType": "DEFAULT"
}, {
"PhoneType": "DDI"
}, {
"PhoneType": "FAX"
}, {
"PhoneType": "MOBILE"
}]
},
"UpdatedDateUTC": "2016-11-19T02:49:47.58",
"IsSupplier": "false",
"IsCustomer": "true"
},
"Date": "2016-11-20T00:00:00",
"DueDate": "2016-11-30T00:00:00",
"ExpectedPaymentDate": "2016-11-30T00:00:00",
"Status": "AUTHORISED",
"LineAmountTypes": "Inclusive",
"LineItems": {
"LineItem": {
"Description": "Test Data 'n n - new linen - new line",
"UnitAmount": "123.00",
"TaxType": "OUTPUT2",
"TaxAmount": "16.04",
"LineAmount": "123.00",
"AccountCode": "260",
"Quantity": "1.0000",
"DiscountRate": "0.00",
"LineItemID": "a4c2da16-4d94-42d6-ae37-e3091901b79b"
}
},
"SubTotal": "106.96",
"TotalTax": "16.04",
"Total": "123.00",
"UpdatedDateUTC": "2016-11-19T23:28:28.607",
"CurrencyCode": "NZD",
"Type": "ACCREC",
"InvoiceID": "cdb5b8e9-afac-4f72-b74b-d9b292295c34",
"InvoiceNumber": "PANDA4323",
"Reference": "test 34",
"AmountDue": "123.00",
"AmountPaid": "0.00",
"SentToContact": "false",
"CurrencyRate": "1.000000",
"TotalDiscount": "0.00"
}
}
}
},
"credits": {
"0": {
"Type": "creditnote",
"UUID": "345d423e-58a6-4e05-8ae5-33da4e886bb9",
"CreditNoteNumber": "CN-0041",
"RemainingCredit": 182.65
},
"availablecredit": 182.65
}
}
}
链接地址: http://www.djcxy.com/p/40669.html
上一篇: String created by JSON.stringify creates error when eval'd by JSON.parse
下一篇: JSON.parse and JSON.stringify are not idempotent and that is bad