简体   繁体   中英

Unexpected string in JSON while parsing XML

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;
});

There's one double quote extra here:

Parse error on line 1:
...\"EOMS_0178_TEST\">""    <name firstName...
-----------------------^
Expecting 'EOF', '}', ':', ',', ']', got 'STRING'

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM