【问题标题】:JavaScript Isolate JSON object inside a StringJavaScript 隔离字符串中的 JSON 对象
【发布时间】:2021-11-25 11:51:43
【问题描述】:

我正在寻求有关我在 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\"}]}};` 

我寻求帮助的文章:

  1. Python 2.7 Isolate multiple JSON objects in a string
  2. What is the correct JSON content type?

【问题讨论】:

    标签: javascript json object logic


    【解决方案1】:

    首先,您需要从错误中获取消息,然后在 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] ]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 2011-10-12
      • 2016-07-16
      相关资源
      最近更新 更多