以适当的格式构造JSON

时间:2014-10-01 10:09:03

标签: javascript json

我的JSON有当前和目标项目。

目标项目在今年剩余时间内保持不变,目前我正在重复所有月份

所以我问专家是否有更好的方法?

它工作正常,但我想在更少的json

中有任何替代方法
{
  "DataContainer": {
    "profit": [
      {
        "current": "20",
        "target": "19"
      },
      {
        "current": "20",
        "target": "19"
      },
      {
        "current": "30",
        "target": "19"
      },
      {
        "current": "23",
        "target": "19"
      },
      {
        "current": "19",
        "target": "19"
      },
      {
        "current": "17",
        "target": "19"
      }
    ],
    "revenue": [
      {
        "current": "50",
        "target": "45"
      },
      {
        "current": "20",
        "target": "45"
      },
      {
        "current": "30",
        "target": "45"
      },
      {
        "current": "23",
        "target": "45"
      },
      {
        "current": "19",
        "target": "45"
      },
      {
        "current": "17",
        "target": "45"
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:1)

重复目标似乎没有多大意义,如果每个对的值相同,为什么不能这样:

{
  "DataContainer": {
    "profit": {"current": ["20","20","30","23","19","17"], "target": "19"},
    "revenue":{"current": ["50","20","30","23","19","17"], "target": "45"}
  }
}

{
  "DataContainer": {
    "profit": {"current": "20 20 30 23 19 17", "target": "19"},
    "revenue":{"current": "50 20 30 23 19 17", "target": "45"}
  }
}

但我不知道你是用它做什么的。