英文:
Unexpected string in JSON while parsing XML
问题
我正在尝试从Oracle数据库中读取CLOB,基本上是XML,然后在AngularJS UI Grid中进行填充。
我用JSON做同样的事情,效果非常好。
来自后端的JSON响应
{
"events": {
"ORDER_NO": "BBY01-100000709660",
"ORDER_HEADER_KEY": "2020040811522311790606",
"CREATETS": "2020-04-08 11:52:47",
"TMPLT_NM": "EOMS_0194",
"EMAIL_XML": "<email CommunicationType=\"Email\" SourceSystem=\"OMS\" TemplatePageZone=\"\" brand=\"BESTBUY\" channel=\"BESTBUY\" emailAddr=\"test.tester@bestbuy.com\" template=\"EOMS_0178_TEST\">..."
}
}
每当我尝试读取值时,它会抛出异常:
JSON中的意外字符串,位置在372
在JSON.parse(<anonymous>
)中
以下是AJAX响应代码:
$http.get(url).then(function(response) {
if (response.data.events == null || response.data.events == undefined || response.data.events == "undefined") {
$("#loader1").hide();
$scope.close = true;
$scope.responseMessage = "";
$scope.gridOptions1.data.length = 0;
$scope.errorMessage = "未找到订单!!!";
} else {
console.log("1");
$("#loader1").hide();
var responseNew = JSON.stringify(response.data.events);
$scope.gridOptions1.data = responseNew;
$scope.mySelectedRows = $scope.gridApi.selection.getSelectedRows();
$scope.close = true;
$scope.errorMessage = "";
$scope.responseMessage = "订单详细信息获取成功";
}
}, function(response) {
$("#loader1").hide();
$scope.close = true;
$scope.responseMessage = "";
$scope.gridOptions.data.length = 0;
$scope.gridOptions1.data.length = 0;
});
英文:
I am trying to read the clob which is basically XML from Oracle DB and populate in AngularJS UI Grid.
I am doing the same with JSON and is working perfectly fine.
JSON response from backend
{"events":{"ORDER_NO":"BBY01-100000709660","ORDER_HEADER_KEY":"2020040811522311790606 ","CREATETS":"2020-04-08 11:52:47","TMPLT_NM":"EOMS_0194 ","EMAIL_XML":"<email CommunicationType=\"Email\" SourceSystem=\"OMS\" TemplatePageZone=\"\" brand=\"BESTBUY\" channel=\"BESTBUY\" emailAddr=\"test.tester@bestbuy.com\" template=\"EOMS_0178_TEST\">"" <name firstName=\"Test\" lastName=\"\" middleInitial=\"\"/>"" <order ATGID=\"ATG28268080246\" IsSuppressRequired=\"Y\" LoggedInFlag=\"Y\" LoyaltyID=\"0160140134\" OrderName=\"MSFTAllAccess\" PartyID=\"123456\" PriorityNumber=\"160140134\" customerPhoneNo=\"6515554321\" hasActivatedDevice=\"N\" orderDate=\"01/28/2020\" orderHeaderKey=\"2020012813423582265743\" orderIdATG=\"BBY01-1MT2010012802\" orderStatusLinkDisplayFlag=\"Y\" orderTotal=\"0.00\" orderTotalMinusCoupons=\"0.00\" partnerID=\"\" partnerOrderNo=\"MAV513281qweq1\" salesSource=\"BBYC\" shippingTotal=\"0.00\" taxTotal=\"0.00\">"" <creditCard cardType=\"\" number=\"\"/>"" <digitalCoupons digitalCouponTotal=\"0.00\"/>"" <lineItems>"" <lineItem CustPromiseDate=\"02/26/2020\" CustPromiseType=\"InHandDate\" availabilityMsg=\"\" beginEstArrivalDate=\"02/24/2020\" conditionVariableOne=\"\" conditionVariableTwo=\"\" description=\"Microsoft Surface Pro 3 12 Intel Core i7 256GB Silver\" endEstArrivalDate=\"02/26/2020\" expectedShipDays=\"\" format=\"\" giftPackaging=\"N\" inHandDate=\"02/26/2020\" itemID=\"\" itemShortDesc=\"Microsoft Surface Pro 3 12 Intel Core i7 256GB Silver\" lineItemProductTotal=\"0.00\" lineItemShippingCost=\"0.00\" merchClass=\"\" modelNo=\"1000186097\" orderLineKey=\"2020021911334791500160\" oversizeFlag=\"\" pickupDate=\"\" preOrder=\"\" primeLine=\"1\" productLine=\"6.403.635\" quantity=\"1\" releaseDate=\"\" reshipReasonCode=\"RESHIP_DAMAGED_ITEM\" shipDate=\"\" shippingMethod=\"\" signatureRequiredFlag=\"N\" sku=\"9248206\" status=\"\" subLine=\"1\" tax=\"0.00\" total=\"0.00\" unitPrice=\"0.00\" unitShippingCost=\"0.00\">"" <shippingAddr city=\"RICHFIELD\" line1=\"1000 W 78TH ST\" line2=\"\" state=\"MN\" zip=\"55423\">"" <name firstName=\"Test\" lastName=\"Tester\" middleInitial=\"\"/>"" </shippingAddr>"" <allowance allowanceAmt=\"0.00\" reason=\"\"/>"" <return date=\"\" lineQty=\"\" lineTotal=\"0.00\" productCredit=\"0.00\" reason=\"\" restockingFee=\"0.00\" shippingCredit=\"0.00\" taxCredit=\"0.00\"/>"" <cancel backOrderExtendedXNumDays=\"\" reason=\"\"/>"" <ros actualDeliveryDate=\"\" pickupDate=\"\"/>"" <store storeName=\"\" storeNum=\"\"/>"" <psp plan=\"\"/>"" <carriers>"" <carrier los=\"\" name=\"\" quantity=\"\" trackingNum=\"\"/>"" </carriers>"" </lineItem>"" </lineItems>"" <makeGood makeGoodFlag=\"N\"/>"" </order>"" <account atgProfileId=\"\" cirisID=\"\" info=\"\" password=\"\"/>"" <comments/>""</email>"}}
Whenever i am trying to read the values it is throwing exception
> Unexpected string in JSON at position 372
at JSON.parse (<anonymous>
)
Below is the AJAX response code:
$http.get(url).then(function(response) {
if(response.data.events == null || response.data.events == undefined ||
response.data.events == "undefined"){
$("#loader1").hide();
$scope.close = true;
$scope.responseMessage = "";
$scope.gridOptions1.data.length=0;
$scope.errorMessage = "Order not found!!!!";
}else{
console.log("1");
$("#loader1").hide();
var responseNew = JSON.stringify(response.data.events);
$scope.gridOptions1.data = responseNew;
$scope.mySelectedRows = $scope.gridApi.selection.getSelectedRows();
$scope.close = true;
$scope.errorMessage = "";
$scope.responseMessage = "Order details fetched successfully";
}
}, function(response) {
$("#loader1").hide();
$scope.close = true;
$scope.responseMessage = "";
$scope.gridOptions.data.length=0;
$scope.gridOptions1.data.length=0;
});
答案1
得分: 0
有一个额外的双引号在这里:
第1行解析错误:
...\"EOMS_0178_TEST\">"" <name firstName...
-----------------------^
期望 'EOF', '}', ':', ',', ']', 得到 'STRING'
英文:
There's one double quote extra here:
Parse error on line 1:
...\"EOMS_0178_TEST\">"" <name firstName...
-----------------------^
Expecting 'EOF', '}', ':', ',', ']', got 'STRING'
答案2
得分: 0
使用 JSON.parse 替代 JSON.stringify。你从后端获取的响应(就是你上面提到的那个)已经是一个字符串化的 JSON,你需要解析它以读取其中的值。
英文:
use JSON.parse instead of JSON.stringify. The response you're getting from back-end (the one you mentioned above) is already a stringified JSON, you have to parse it out to read the values.
答案3
得分: 0
以上问题出现在将XML存储在数据库中时。由于新元素之间有空格,它将其视为字符串,并在JSON中附加了双引号。
英文:
The above issue waswhile storing the xml in DB. since the new elements had spaces in between. it was considering that as a string and was getting appended with double quotes in JSON.
专注分享java语言的经验与见解,让所有开发者获益!
评论