所以我有以下回應:
{
"errors": [
{
"errorKey": "ERROR_NO_DELIVERY_OPTIONS",
"errorParameters": "[{\"errorMessage\":\"ERROR_DELIVERY_OPTIONS_YOU_SELECTED_NOT_AVAILABLE_NOW\",\"partNumbers\":[\"19308033\",\"19114798\"]},{\"errorMessage\":\"Pickup At Seller not available for these orderItemIds\",\"orderItemIds\":[\"10315031\",\"10315032\"],\"availableShipModeId\":\"13201\"}]",
"errorMessage": "ERROR_NO_DELIVERY_OPTIONS",
"errorCode": "ERROR_NO_DELIVERY_OPTIONS"
}
]
}
不幸的是,我不確定如何使用“errorParameters”的值,因為它只是一個字串,而不是像其他人一樣的簡單鍵值。我將如何提取所有資訊以便我可以使用它。一位同事提到決議它,但不確定他的意思是什么以及如何決議。下面是一個更具可讀性的值。我正在使用 javascript。
[
{
"errorMessage": "ERROR_DELIVERY_OPTIONS_YOU_SELECTED_NOT_AVAILABLE_NOW",
"partNumbers":
[
19308033,
19114798
]
},
{
"errorMessage": "No Shipmodes Available for these orderItemsIds",
"orderItemIds": [
10315031,
10315032
]
}
]
uj5u.com熱心網友回復:
您將需要使用JSON.parse將 JSON 字串轉換為 JS 物件。
您需要在代碼中執行此操作。IE
data.errors.forEach(e => console.log(JSON.parse(e.errorParameters)))
uj5u.com熱心網友回復:
您可以只使用JSON.parse()函式,它將字串 JSON 表示形式更改為 JavaScript 物件。
const parsedErrorParameters = JSON.parse(data.errorParameters);
console.log(parsedErrorParameters[0].errorMessage); // ERROR_DELIVERY_OPTIONS_YOU_SELECTED_NOT_AVAILABLE_NOW
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/328367.html
標籤:javascript json 解析
