我正在尋求有關我在 Azure 邏輯應用程式中遇到的問題的幫助。我有一個由失敗的 HTTP 請求提供的 JSON 輸出,如下所示:
{
"error": {
"code": "",
"message": "Http request failed with statusCode=BadRequest : {\"error\":{\"code\":\"ValidationFailed\",\"message\":\"Validation of indicator content failed.\",\"target\":\"body\",\"details\":[{\"code\":\"ValidationFailed\",\"message\":\"The value 1 is not a valid domain name.\",\"target\":\"domainName\"}]}}; ",
"innerError": {
"date": "2021-11-25T11:12:09",
"request-id": "",
"client-request-id": ""
}
}
}
我打算做的是隔離"message"價值,以告知我的應用程式的用戶應用程式失敗的原因。我相信 的值"message"是一個字串,但是,它也包含一個 JSON 物件。如果我可以轉義物件兩側的字符,我就可以決議 JSON 并選擇該物件中的值。
這是我希望能夠訪問的物件的更好視圖:
{
"error": {
"code": "ValidationFailed",
"message": "Validation of indicator content failed.",
"target": "body",
"details": [
{
"code": "ValidationFailed",
"message": "The value 1 is not a valid domain name.",
"target": "domainName"
}
]
}
}
如何訪問這些值?我能夠在我的邏輯應用程式中使用 JavaScript,所以如果通過使用 JS 有一個簡單的解決方案,那就太好了。
我目前正在決議初始 JSON 物件,顯示在這篇文章的頂部,然后決議"message"值以嘗試訪問這些變數。然而,這不起作用,因為值"message"不是物件。
如果我能夠訪問嵌套"code"并"message"輸出下面的內容,那就太好了。
ValidationFailed: Validation of indicator content failed
或者更好..
ValidationFailed: Validation of indicator content failed - The value 1 is not a valid domain name.
感謝您對此提供的任何幫助。我目前沒有 JavaScript 解決方案,我試圖純粹使用邏輯應用程式來解決這個問題。Azure 允許行內 JavaScript 在應用程式中執行,因此任何 JavaScript 解決方案都會有所幫助。
如果需要更多關于此的資訊,請告訴我,在決議 JSON 之前洗掉下面的粗體文本應該允許我在應用程式中使用該物件。
Http 請求失敗, statusCode=BadRequest : `
離開物件..
{\"error\":{\"code\":\"ValidationFailed\",\"message\":\"Validation of indicator content failed.\",\"target\":\"body\",\"details\":[{\"code\":\"ValidationFailed\",\"message\":\"The value 1 is not a valid domain name.\",\"target\":\"domainName\"}]}};`
我已經研究過的文章尋求幫助:
- Python 2.7 在一個字串中隔離多個 JSON 物件
- 什么是正確的 JSON 內容型別?
uj5u.com熱心網友回復:
首先,您需要從錯誤中獲取訊息,然后在 Message 屬性中獲取帶有花括號的值。
這是我創建的用于提取錯誤物件的簡單代碼行。
const data = {
"error": {
"code": "",
"message": "Http request failed with statusCode=BadRequest : {\"error\":{\"code\":\"ValidationFailed\",\"message\":\"Validation of indicator content failed.\",\"target\":\"body\",\"details\":[{\"code\":\"ValidationFailed\",\"message\":\"The value 1 is not a valid domain name.\",\"target\":\"domainName\"}]}}; ",
"innerError": {
"date": "2021-11-25T11:12:09",
"request-id": "",
"client-request-id": ""
}
}
};
console.log(JSON.parse(data.error.message.match('({.*})')[1]));
這console.log將以這種格式列印結果:
{
error: {
code: 'ValidationFailed',
message: 'Validation of indicator content failed.',
target: 'body',
details: [ [Object] ]
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/366627.html
標籤:javascript json 目的 逻辑
