【问题标题】:JSON stringify not working as expected while trying to extract a response尝试提取响应时,JSON stringify 无法按预期工作
【发布时间】:2021-07-14 12:08:07
【问题描述】:

我有这样格式的回复:

我想提取error 键,我尝试了以下代码:

  console.log('errorResponse', JSON.stringify(errRes.error)).

返回

"javax.xml.rpc.ServiceException: [{\"ReturnCode\":0,\"counter\":\"\\\\\\\\UCM1-PUB.ciscoctg.com\\\\Cisco CallManager\\\\AnnunciatorOutOfResources\"},{\"ReturnCode\":0,\"counter\":\"\\\\\\\\UCM1-SUB-1A.ciscoctg.com\\\\Cisco CallManager\\\\AnnunciatorOutOfResources\"},{\"ReturnCode\":101,\"ErrStr\":\"\\\\\\\\IMP1-SUB-1B.ciscoctg.com\\\\Cisco CallManager\\\\AnnunciatorOutOfResources;\",\"counter\":\"\\\\\\\\IMP1-SUB-1B.ciscoctg.com\\\\Cisco CallManager\\\\AnnunciatorOutOfResources\"}]"

但是当我尝试从 error 键中过滤掉 ReturnCode 不是 0 的键时,我得到了错误:

ERROR TypeError: errRes.error.filter is not a function

我试过了:

console.log('errorResponse', JSON.stringify(errRes.error.filter(val => val.ReturnCode !== 0)))

我不知道如何提取它。请帮忙。提前致谢。

编辑 1:

编辑 2:

【问题讨论】:

  • 这与JSON.stringify()angular 有什么关系?
  • .error的内容是一个字符串。为什么.filter() 应该在这种情况下工作?
  • 请告诉我这个console.log(errRes.error)console.log(typeof errRes.error)的输出
  • @0brine 我在编辑中添加了它,它打印了 2 次,第二次显示 undefined。正如您在屏幕截图中看到的那样,同一行代码打印了 2 次​​span>

标签: javascript angular


【解决方案1】:

请使用此方法 警报(JSON.stringify(error.json()));

【讨论】:

    【解决方案2】:

    试试这个,我知道为什么,但它似乎工作 JSON.parse(JSON.stringify(errRes.error).replace("javax.xml.rpc.ServiceException: ", "")).filter(...)

    【讨论】:

    • 感谢您的回答。我试过了,但它给出了这个错误:core.js:6162 ERROR TypeError: JSON.parse(...).filter is not a function
    • 好的,请给我看console.log(JSON.parse(JSON.stringify(errRes.error).replace("javax.xml.rpc.ServiceException: ", "")))的输出
    • 我在Edit 2中更新并添加了上面的截图。它打印 2 次,第一次打印输出,第二次未定义。两者都在屏幕截图中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多