【问题标题】:Replacing null and undefined values in json value with empty("") [duplicate]用空(“”)替换json值中的空值和未定义值[重复]
【发布时间】:2016-08-12 23:04:19
【问题描述】:

我收到包含大量 nullundefined 值的 JSON 响应。我需要用空字符串 ("") 替换所有 nullundefined 值。以下是我收到的示例回复:

{
  lecCicode: null
  lecOrderNo: null
  loccType: "Z"
  nccrCode: null
  nciCode: null
},
{
  lecCicode: null
  lecOrderNo: null
  loccType: "Z"
  nccrCode: null
  nciCode: null
},
{
  lecCicode: null
  lecOrderNo: null
  loccType: "Z"
  nccrCode: null
  nciCode: null
}

【问题讨论】:

  • Array#forEachObject.keys ?
  • “我得到 json 响应,其中包含大量空值和未定义值” 您可能会收到 null,但您肯定不会收到 undefined,因为它没有'在 JSON 中不存在。
  • null 作为值有什么问题???
  • DATA.forEach(function(item) { Object.keys(item).forEach(function(key) { item[key] = item[key] == null ? '' : item[key]; }) });

标签: javascript jquery json ajax


【解决方案1】:

你可以试试:

JSON.parse(JSON.stringify(yourjson).replace(/null/g, '""'))

【讨论】:

  • 如果键的名称是"null" 或者值是"I am null" 那么?
  • 那么上述解决方案将不起作用,上述解决方案仅用于从值中替换 null,如问题中所示
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 2017-10-21
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
相关资源
最近更新 更多