在解析XML时出现了JSON中的意外字符串。

huangapple 未分类评论44阅读模式
英文:

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

{&quot;events&quot;:{&quot;ORDER_NO&quot;:&quot;BBY01-100000709660&quot;,&quot;ORDER_HEADER_KEY&quot;:&quot;2020040811522311790606  &quot;,&quot;CREATETS&quot;:&quot;2020-04-08 11:52:47&quot;,&quot;TMPLT_NM&quot;:&quot;EOMS_0194                               &quot;,&quot;EMAIL_XML&quot;:&quot;&lt;email CommunicationType=\&quot;Email\&quot; SourceSystem=\&quot;OMS\&quot; TemplatePageZone=\&quot;\&quot; brand=\&quot;BESTBUY\&quot; channel=\&quot;BESTBUY\&quot; emailAddr=\&quot;test.tester@bestbuy.com\&quot; template=\&quot;EOMS_0178_TEST\&quot;&gt;&quot;&quot;    &lt;name firstName=\&quot;Test\&quot; lastName=\&quot;\&quot; middleInitial=\&quot;\&quot;/&gt;&quot;&quot;    &lt;order ATGID=\&quot;ATG28268080246\&quot; IsSuppressRequired=\&quot;Y\&quot; LoggedInFlag=\&quot;Y\&quot; LoyaltyID=\&quot;0160140134\&quot; OrderName=\&quot;MSFTAllAccess\&quot; PartyID=\&quot;123456\&quot; PriorityNumber=\&quot;160140134\&quot; customerPhoneNo=\&quot;6515554321\&quot; hasActivatedDevice=\&quot;N\&quot; orderDate=\&quot;01/28/2020\&quot; orderHeaderKey=\&quot;2020012813423582265743\&quot; orderIdATG=\&quot;BBY01-1MT2010012802\&quot; orderStatusLinkDisplayFlag=\&quot;Y\&quot; orderTotal=\&quot;0.00\&quot; orderTotalMinusCoupons=\&quot;0.00\&quot; partnerID=\&quot;\&quot; partnerOrderNo=\&quot;MAV513281qweq1\&quot; salesSource=\&quot;BBYC\&quot; shippingTotal=\&quot;0.00\&quot; taxTotal=\&quot;0.00\&quot;&gt;&quot;&quot;        &lt;creditCard cardType=\&quot;\&quot; number=\&quot;\&quot;/&gt;&quot;&quot;        &lt;digitalCoupons digitalCouponTotal=\&quot;0.00\&quot;/&gt;&quot;&quot;        &lt;lineItems&gt;&quot;&quot;            &lt;lineItem CustPromiseDate=\&quot;02/26/2020\&quot; CustPromiseType=\&quot;InHandDate\&quot; availabilityMsg=\&quot;\&quot; beginEstArrivalDate=\&quot;02/24/2020\&quot; conditionVariableOne=\&quot;\&quot; conditionVariableTwo=\&quot;\&quot; description=\&quot;Microsoft  Surface Pro 3  12  Intel Core i7  256GB  Silver\&quot; endEstArrivalDate=\&quot;02/26/2020\&quot; expectedShipDays=\&quot;\&quot; format=\&quot;\&quot; giftPackaging=\&quot;N\&quot; inHandDate=\&quot;02/26/2020\&quot; itemID=\&quot;\&quot; itemShortDesc=\&quot;Microsoft  Surface Pro 3  12  Intel Core i7  256GB  Silver\&quot; lineItemProductTotal=\&quot;0.00\&quot; lineItemShippingCost=\&quot;0.00\&quot; merchClass=\&quot;\&quot; modelNo=\&quot;1000186097\&quot; orderLineKey=\&quot;2020021911334791500160\&quot; oversizeFlag=\&quot;\&quot; pickupDate=\&quot;\&quot; preOrder=\&quot;\&quot; primeLine=\&quot;1\&quot; productLine=\&quot;6.403.635\&quot; quantity=\&quot;1\&quot; releaseDate=\&quot;\&quot; reshipReasonCode=\&quot;RESHIP_DAMAGED_ITEM\&quot; shipDate=\&quot;\&quot; shippingMethod=\&quot;\&quot; signatureRequiredFlag=\&quot;N\&quot; sku=\&quot;9248206\&quot; status=\&quot;\&quot; subLine=\&quot;1\&quot; tax=\&quot;0.00\&quot; total=\&quot;0.00\&quot; unitPrice=\&quot;0.00\&quot; unitShippingCost=\&quot;0.00\&quot;&gt;&quot;&quot;                &lt;shippingAddr city=\&quot;RICHFIELD\&quot; line1=\&quot;1000 W 78TH ST\&quot; line2=\&quot;\&quot; state=\&quot;MN\&quot; zip=\&quot;55423\&quot;&gt;&quot;&quot;                    &lt;name firstName=\&quot;Test\&quot; lastName=\&quot;Tester\&quot; middleInitial=\&quot;\&quot;/&gt;&quot;&quot;                &lt;/shippingAddr&gt;&quot;&quot;                &lt;allowance allowanceAmt=\&quot;0.00\&quot; reason=\&quot;\&quot;/&gt;&quot;&quot;                &lt;return date=\&quot;\&quot; lineQty=\&quot;\&quot; lineTotal=\&quot;0.00\&quot; productCredit=\&quot;0.00\&quot; reason=\&quot;\&quot; restockingFee=\&quot;0.00\&quot; shippingCredit=\&quot;0.00\&quot; taxCredit=\&quot;0.00\&quot;/&gt;&quot;&quot;                &lt;cancel backOrderExtendedXNumDays=\&quot;\&quot; reason=\&quot;\&quot;/&gt;&quot;&quot;                &lt;ros actualDeliveryDate=\&quot;\&quot; pickupDate=\&quot;\&quot;/&gt;&quot;&quot;                &lt;store storeName=\&quot;\&quot; storeNum=\&quot;\&quot;/&gt;&quot;&quot;                &lt;psp plan=\&quot;\&quot;/&gt;&quot;&quot;                &lt;carriers&gt;&quot;&quot;                    &lt;carrier los=\&quot;\&quot; name=\&quot;\&quot; quantity=\&quot;\&quot; trackingNum=\&quot;\&quot;/&gt;&quot;&quot;                &lt;/carriers&gt;&quot;&quot;            &lt;/lineItem&gt;&quot;&quot;        &lt;/lineItems&gt;&quot;&quot;        &lt;makeGood makeGoodFlag=\&quot;N\&quot;/&gt;&quot;&quot;    &lt;/order&gt;&quot;&quot;    &lt;account atgProfileId=\&quot;\&quot; cirisID=\&quot;\&quot; info=\&quot;\&quot; password=\&quot;\&quot;/&gt;&quot;&quot;    &lt;comments/&gt;&quot;&quot;&lt;/email&gt;&quot;}}

Whenever i am trying to read the values it is throwing exception

> Unexpected string in JSON at position 372
at JSON.parse (&lt;anonymous&gt;)

Below is the AJAX response code:

$http.get(url).then(function(response) {
	if(response.data.events == null || response.data.events == undefined ||
			response.data.events == &quot;undefined&quot;){
		$(&quot;#loader1&quot;).hide();
        $scope.close = true;
        $scope.responseMessage = &quot;&quot;;
        $scope.gridOptions1.data.length=0;
        $scope.errorMessage = &quot;Order not found!!!!&quot;;
	}else{
	    console.log(&quot;1&quot;);
	    $(&quot;#loader1&quot;).hide();
	    var responseNew = JSON.stringify(response.data.events);
		$scope.gridOptions1.data = responseNew;
        $scope.mySelectedRows = $scope.gridApi.selection.getSelectedRows();
        $scope.close = true;
        $scope.errorMessage = &quot;&quot;;
        $scope.responseMessage = &quot;Order details fetched successfully&quot;;
	}
}, function(response) {
    $(&quot;#loader1&quot;).hide();
    $scope.close = true;
    $scope.responseMessage = &quot;&quot;;
    $scope.gridOptions.data.length=0;
    $scope.gridOptions1.data.length=0;
});

答案1

得分: 0

有一个额外的双引号在这里:

第1行解析错误:
...\&quot;EOMS_0178_TEST\&quot;&gt;&quot;&quot;    &lt;name firstName...
-----------------------^
期望 &#39;EOF&#39;, &#39;}&#39;, &#39;:&#39;, &#39;,&#39;, &#39;]&#39;, 得到 &#39;STRING&#39;
英文:

There's one double quote extra here:

Parse error on line 1:
...\&quot;EOMS_0178_TEST\&quot;&gt;&quot;&quot;    &lt;name firstName...
-----------------------^
Expecting &#39;EOF&#39;, &#39;}&#39;, &#39;:&#39;, &#39;,&#39;, &#39;]&#39;, got &#39;STRING&#39;

答案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.

huangapple
  • 本文由 发表于 2020年4月9日 19:31:37
  • 转载请务必保留本文链接:https://java.coder-hub.com/61120177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定