迭代并读取json响应

时间:2014-11-13 20:29:04

标签: javascript jquery json

我有一个json回复

{"profits":{"1":"0,00","2":"0,00","3":"0,00","4":"0,00","5":"0,00","6":"0,00","7":"1.582,64","8":"0,00","9":"0,00","10":"1.500,00","11":"6.000,00","12":"0,00"},"costs":{"1":"0,00","2":"0,00","3":"0,00","4":"100,00","5":"0,00","6":"0,00","7":"0,00","8":"0,00","9":"0,00","10":"123,14","11":"826,45","12":"0,00"},"result":{"1":"0,00","2":"0,00","3":"0,00","4":"-100,00","5":"0,00","6":"0,00","7":"1.582,64","8":"0,00","9":"0,00","10":"1.376,86","11":"5.173,55","12":"0,00"},"total_profits":"9.082,64","total_costs":"1.049,59","total":"8.033,05"}

我需要利润的第七个关键值。我试过这个:

var json = $.parseJSON(data);
console.log(json.profits.7);

然后我在控制台日志中收到了一个意外的号码。我该如何解决?

1 个答案:

答案 0 :(得分:1)

json.profits['7']

一起使用



var json = {
  "profits": {
    "1": "0,00",
    "2": "0,00",
    "3": "0,00",
    "4": "0,00",
    "5": "0,00",
    "6": "0,00",
    "7": "1.582,64",
    "8": "0,00",
    "9": "0,00",
    "10": "1.500,00",
    "11": "6.000,00",
    "12": "0,00"
  },
  "costs": {
    "1": "0,00",
    "2": "0,00",
    "3": "0,00",
    "4": "100,00",
    "5": "0,00",
    "6": "0,00",
    "7": "0,00",
    "8": "0,00",
    "9": "0,00",
    "10": "123,14",
    "11": "826,45",
    "12": "0,00"
  },
  "result": {
    "1": "0,00",
    "2": "0,00",
    "3": "0,00",
    "4": "-100,00",
    "5": "0,00",
    "6": "0,00",
    "7": "1.582,64",
    "8": "0,00",
    "9": "0,00",
    "10": "1.376,86",
    "11": "5.173,55",
    "12": "0,00"
  },
  "total_profits": "9.082,64",
  "total_costs": "1.049,59",
  "total": "8.033,05"
};

alert(json.profits['7']);