从GET nodejs解析json

时间:2018-08-23 08:11:44

标签: javascript json node.js

我通过ajax调用得到了

{
  "order": 0,
  "template": "helloindex",
  "settings": {
    "index": {
      "codec": "best_compression",
      "refresh_interval": "60s",
      "number_of_shards": "10",
      "number_of_replicas": "1"
    }
  },
  "mappings": {
    "_default_": {
      "_all": {
        "enabled": false
      },
      "properties": {
        "UNDERLYING": {
          "type": "keyword"
        },
        "SERVICE": {
          "type": "integer"
        },
        "CLIENT": {
          "type": "integer"
        },
        "VALUATIONDATE": {
          "type": "date",
          "format": "MM/dd/yyyy"
        },
        "COUNTRY": {
          "type": "keyword"
        }
      }
    }
  },
  "aliases": {}
}

我想将其转换为json对象,但是我无法从中得到任何东西。

尝试使用JSON.parse,但缺少某些内容。

2 个答案:

答案 0 :(得分:0)

如果对象看起来像这样,则可以进行一些进一步的解析:

let obj = { '0': '{"field":"doc_no","cond":"is","val":"","main_cond":"and"}',
  '1': '{"field":"doc_no","cond":"is","val":"","main_cond":"and"}' };
  
for(let p in obj) {
    obj[p] = JSON.parse(obj[p]);
}

console.log(obj);

答案 1 :(得分:0)

{ '0': '{"field":"doc_no","cond":"is","val":"","main_cond":"and"}',
  '1': '{"field":"doc_no","cond":"is","val":"","main_cond":"and"}' }

这是无效的JSON字符串。正确的是,例如:

var parseMe = ' { "0": { "field": "doc_no", "cond": "is", "val": "", "main_cond": "and" },"1":{"field": "doc_no","cond": "is", "val": "", "main_cond": "and" }}';
console.log(JSON.parse(parseMe));