将JSON转换为强类型对象

时间:2017-11-14 04:17:19

标签: javascript json

我收到以下作为回复,我想将其转换为对象。

WebSQLRows {
  "_array": Array [
    Object {
      "bmi": 24.7,
      "id": 1,
      "imperialgoalweight": 154.3,
      "imperialheight": 70.9,
      "imperialweight": 176.4,
      "metricgoalweight": 70,
      "metricheight": 180,
      "metricweight": 80,
      "standard": "Metric",
    },
    Object {
      "bmi": 24.7,
      "id": 2,
      "imperialgoalweight": 154.3,
      "imperialheight": 70.9,
      "imperialweight": 176.4,
      "metricgoalweight": 70,
      "metricheight": 180,
      "metricweight": 80,
      "standard": "Metric",
    },
    Object {
      "bmi": 26.2,
      "id": 3,
      "imperialgoalweight": 154.3,
      "imperialheight": 70.9,
      "imperialweight": 187.4,
      "metricgoalweight": 70,
      "metricheight": 180,
      "metricweight": 85,
      "standard": "Metric",
    },
  ],
  "length": 3,
}

我尝试使用JSON.parse()但没有运气。 关于如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:1)

您提供的文本可能是您从控制台获得的文本。在这种情况下,只需删除尾随的逗号。

否则需要通过删除尾随逗号和数据类型(蓝色字)来修改文本

以下是有效的JSON:

{
      "_array":  [
         {
          "bmi": 24.7,
          "id": 1,
          "imperialgoalweight": 154.3,
          "imperialheight": 70.9,
          "imperialweight": 176.4,
          "metricgoalweight": 70,
          "metricheight": 180,
          "metricweight": 80,
          "standard": "Metric"
        },
         {
          "bmi": 24.7,
          "id": 2,
          "imperialgoalweight": 154.3,
          "imperialheight": 70.9,
          "imperialweight": 176.4,
          "metricgoalweight": 70,
          "metricheight": 180,
          "metricweight": 80,
          "standard": "Metric"
        },
         {
          "bmi": 26.2,
          "id": 3,
          "imperialgoalweight": 154.3,
          "imperialheight": 70.9,
          "imperialweight": 187.4,
          "metricgoalweight": 70,
          "metricheight": 180,
          "metricweight": 85,
          "standard": "Metric"
        }
      ],
      "length": 3
}