使用 JavaScript 从 JSON 获取值,键名中有 space
Getting value from JSON, which has space in key name using JavaScript
{
"response": {
"success": 1,
"current_time": 1440089067,
"raw_usd_value": 0.125,
"usd_currency": "metal",
"usd_currency_index": 5002,
"items": {
"A Brush with Death": {
"defindex": [
30186
],
"prices": {
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 4,
"last_update": 1436704678,
"difference": -0.5
}
}
}
}
}
},
"A Color Similar to Slate": {
"defindex": [
5052
],
"prices": {
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 6,
"last_update": 1430635440,
"difference": 0.5,
"value_high": 7
}
}
}
}
}
},
"A Deep Commitment to Purple": {
"defindex": [
5031
],
"prices": {
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 6.66,
"last_update": 1439385956,
"difference": -0.44,
"value_high": 7
}
}
}
}
}
},
"A Distinctive Lack of Hue": {
"defindex": [
5040
],
"prices": {
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "keys",
"value": 2,
"last_update": 1440027536,
"difference": 2.4
}
}
}
}
}
},
"A Mann's Mint": {
"defindex": [
5076
],
"prices": {
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 14.33,
"last_update": 1438838853,
"difference": -1.61
}
}
}
}
}
},
"A Random Duck Token Gift": {
"defindex": [
5780
],
"prices": {
"6": {
"Tradable": {
"Non-Craftable": {
"0": {
"currency": "keys",
"value": 3,
"last_update": 1426634734,
"difference": -8.5825
}
}
}
}
}
},
"A Random End of the Line Key Gift": {
"defindex": [
5777
],
"prices": {
"6": {
"Tradable": {
"Non-Craftable": {
"0": {
"currency": "keys",
"value": 1,
"last_update": 1419626119,
"difference": -0.42075
}
}
}
}
}
},
"A Rather Festive Tree": {
"defindex": [
341
],
"prices": {
"5": {
"Tradable": {
"Craftable": {
"10": {
"currency": "keys",
"value": 61,
"last_update": 1432251950,
"difference": -380
},
"11": {
"currency": "keys",
"value": 21,
"last_update": 1435842513,
"difference": -95.275
},
"12": {
"currency": "keys",
"value": 16,
"last_update": 1413933108,
"difference": -37.2328125,
"value_high": 20
},
"13": {
"currency": "keys",
"value": 130,
"last_update": 1439447170,
"difference": 144
},
"14": {
"currency": "keys",
"value": 108,
"last_update": 1430391363,
"difference": 162,
"value_high": 114
},
"15": {
"currency": "keys",
"value": 40,
"last_update": 1433252039,
"difference": -95
},
"16": {
"currency": "keys",
"value": 44,
"last_update": 1437784980,
"difference": 39.425,
"value_high": 54
},
"17": {
"currency": "keys",
"value": 90,
"last_update": 1434161864,
"difference": 438.265,
"value_high": 100
},
"18": {
"currency": "keys",
"value": 28,
"last_update": 1433606334,
"difference": -114.33
},
"19": {
"currency": "keys",
"value": 52,
"last_update": 1421180874,
"difference": -69.11625
},
"29": {
"currency": "keys",
"value": 24,
"last_update": 1429293873,
"difference": -128.7375,
"value_high": 29
},
"30": {
"currency": "keys",
"value": 30,
"last_update": 1437869667,
"difference": -39.425,
"value_high": 40
},
"31": {
"currency": "keys",
"value": 18,
"last_update": 1402168429,
"difference": -7.0224375,
"value_high": 21
},
"32": {
"currency": "keys",
"value": 19,
"last_update": 1431897611,
"difference": -38,
"value_high": 21
},
"33": {
"currency": "keys",
"value": 16,
"last_update": 1438978898,
"difference": 56,
"value_high": 21
},
"34": {
"currency": "keys",
"value": 14,
"last_update": 1432783358,
"difference": -57,
"value_high": 19
},
"35": {
"currency": "keys",
"value": 20,
"last_update": 1439667519,
"difference": -48,
"value_high": 23
},
"36": {
"currency": "keys",
"value": 16,
"last_update": 1416523879,
"difference": -24.8994375,
"value_high": 19
},
"37": {
"currency": "usd",
"value": 165.31,
"value_high": 188.92,
"last_update": 1364519581,
"difference": -26.349
},
"38": {
"currency": "keys",
"value": 110,
"last_update": 1438115127,
"difference": 86.735,
"value_high": 115
},
"39": {
"currency": "keys",
"value": 31,
"last_update": 1421127642,
"difference": -47.723125,
"value_high": 43
},
"40": {
"currency": "keys",
"value": 31,
"last_update": 1426044571,
"difference": -9.2565000000002,
"value_high": 37
},
"43": {
"currency": "keys",
"value": 85,
"last_update": 1419053757,
"difference": 204.77272727273,
"value_high": 88
},
"44": {
"currency": "keys",
"value": 137,
"last_update": 1433875578,
"difference": 444.25166666667,
"value_high": 157
},
"45": {
"currency": "usd",
"value": 649.77,
"last_update": 1375636025,
"difference": 561.793
},
"46": {
"currency": "keys",
"value": 153,
"last_update": 1401752526,
"difference": -13.394387755102
},
"47": {
"currency": "keys",
"value": 79,
"last_update": 1425084672,
"difference": -315.86442857143,
"value_high": 93
},
"56": {
"currency": "keys",
"value": 22,
"last_update": 1436215169,
"difference": -179.4075,
"value_high": 26
},
"57": {
"currency": "keys",
"value": 40,
"last_update": 1422406359,
"difference": -58.086
},
"58": {
"currency": "keys",
"value": 28,
"last_update": 1417887466,
"difference": -177.24,
"value_high": 31
},
"59": {
"currency": "keys",
"value": 23,
"last_update": 1436452318,
"difference": -94.425,
"value_high": 29
},
"6": {
"currency": "keys",
"value": 22,
"last_update": 1423766468,
"difference": -28.33125,
"value_high": 25
},
"60": {
"currency": "keys",
"value": 16,
"last_update": 1431406119,
"difference": -122.395,
"value_high": 22
},
"61": {
"currency": "keys",
"value": 25,
"last_update": 1428174196,
"difference": -664.9875,
"value_high": 30
},
"62": {
"currency": "keys",
"value": 40,
"last_update": 1439097289,
"difference": -72,
"value_high": 41
},
"63": {
"currency": "keys",
"value": 70,
"last_update": 1431721568,
"difference": -247
},
"67": {
"currency": "keys",
"value": 90,
"last_update": 1402459658,
"difference": 793.5354375,
"value_high": 99
},
"68": {
"currency": "keys",
"value": 63,
"last_update": 1434103924,
"difference": -133.385
},
"69": {
"currency": "keys",
"value": 70,
"last_update": 1434472713,
"difference": 171.495,
"value_high": 90
},
"7": {
"currency": "keys",
"value": 20,
"last_update": 1419464617,
"difference": 17.24625,
"value_high": 25
},
"71": {
"currency": "keys",
"value": 90,
"last_update": 1434448204,
"difference": 171.495
},
"75": {
"currency": "usd",
"value": 217.11,
"value_high": 290.49,
"last_update": 1384832245,
"difference": 0
},
"79": {
"currency": "keys",
"value": 75,
"last_update": 1439565200,
"difference": -168
},
"8": {
"currency": "keys",
"value": 48,
"last_update": 1421187499,
"difference": -49.36875,
"value_high": 56
},
"9": {
"currency": "keys",
"value": 41,
"last_update": 1431063296,
"difference": -103.565,
"value_high": 53
},
"73": {
"currency": "keys",
"value": 169,
"last_update": 1420050012,
"difference": 278.93125,
"value_high": 195
},
"78": {
"currency": "keys",
"value": 75,
"last_update": 1423246411,
"difference": -18.8875
},
"70": {
"currency": "keys",
"value": 91,
"last_update": 1403662920,
"difference": 847.51875,
"value_high": 108
},
"65": {
"currency": "keys",
"value": 64,
"last_update": 1411708482,
"difference": 555.883
},
"83": {
"currency": "keys",
"value": 80,
"last_update": 1418974322,
"difference": 1146.25,
"value_high": 84
},
"86": {
"currency": "keys",
"value": 70,
"last_update": 1419116750,
"difference": 992.25
},
"81": {
"currency": "keys",
"value": 133,
"last_update": 1421127817,
"difference": 1861.201875,
"value_high": 150
},
"88": {
"currency": "keys",
"value": 40,
"last_update": 1434472710,
"difference": 304.88,
"value_high": 50
},
"87": {
"currency": "keys",
"value": 122,
"last_update": 1427764322,
"difference": 2325.6
},
"90": {
"currency": "keys",
"value": 62,
"last_update": 1431609884,
"difference": 1167.46
}
}
}
},
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 1.66,
"last_update": 1425877431,
"difference": -0.11
}
},
"Non-Craftable": {
"0": {
"currency": "metal",
"value": 1.55,
"last_update": 1440003716,
"difference": -0.11
}
}
}
}
}
},
"A Whiff of the Old Brimstone": {
"defindex": [
610
],
"prices": {
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 1.44,
"last_update": 1437527131,
"difference": -0.055
}
},
"Non-Craftable": {
"0": {
"currency": "metal",
"value": 0.88,
"last_update": 1438115814,
"difference": -0.12
}
}
}
}
}
},
"AWPer Hand": {
"defindex": [
851
],
"prices": {
"1": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 9.33,
"last_update": 1434818573,
"difference": -0.335,
"value_high": 10
}
}
}
},
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 2.88,
"last_update": 1438022976,
"difference": -0.06,
"value_high": 3
}
}
}
},
"11": {
"Tradable": {
"Craftable": {
"0": {
"currency": "keys",
"value": 9,
"last_update": 1439223013,
"difference": -4
}
}
}
}
}
}
}
}
}
我在从 JSON 获取值时遇到问题,该值在键名中包含 space。我正在使用 JavaScript 来获取值,但如果密钥包含 space(与死亡擦肩而过)或者它是一个数字 (6),我将无法使其工作。
有什么办法可以不编辑 JSON 文件吗?
提前致谢。 :)
有两种访问属性的方法;
foo.bar
foo['bar']
你需要使用第二个选项来使用空格,例如foo['bar bar'].baz
{
"response": {
"success": 1,
"current_time": 1440089067,
"raw_usd_value": 0.125,
"usd_currency": "metal",
"usd_currency_index": 5002,
"items": {
"A Brush with Death": {
"defindex": [
30186
],
"prices": {
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 4,
"last_update": 1436704678,
"difference": -0.5
}
}
}
}
}
},
"A Color Similar to Slate": {
"defindex": [
5052
],
"prices": {
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 6,
"last_update": 1430635440,
"difference": 0.5,
"value_high": 7
}
}
}
}
}
},
"A Deep Commitment to Purple": {
"defindex": [
5031
],
"prices": {
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 6.66,
"last_update": 1439385956,
"difference": -0.44,
"value_high": 7
}
}
}
}
}
},
"A Distinctive Lack of Hue": {
"defindex": [
5040
],
"prices": {
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "keys",
"value": 2,
"last_update": 1440027536,
"difference": 2.4
}
}
}
}
}
},
"A Mann's Mint": {
"defindex": [
5076
],
"prices": {
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 14.33,
"last_update": 1438838853,
"difference": -1.61
}
}
}
}
}
},
"A Random Duck Token Gift": {
"defindex": [
5780
],
"prices": {
"6": {
"Tradable": {
"Non-Craftable": {
"0": {
"currency": "keys",
"value": 3,
"last_update": 1426634734,
"difference": -8.5825
}
}
}
}
}
},
"A Random End of the Line Key Gift": {
"defindex": [
5777
],
"prices": {
"6": {
"Tradable": {
"Non-Craftable": {
"0": {
"currency": "keys",
"value": 1,
"last_update": 1419626119,
"difference": -0.42075
}
}
}
}
}
},
"A Rather Festive Tree": {
"defindex": [
341
],
"prices": {
"5": {
"Tradable": {
"Craftable": {
"10": {
"currency": "keys",
"value": 61,
"last_update": 1432251950,
"difference": -380
},
"11": {
"currency": "keys",
"value": 21,
"last_update": 1435842513,
"difference": -95.275
},
"12": {
"currency": "keys",
"value": 16,
"last_update": 1413933108,
"difference": -37.2328125,
"value_high": 20
},
"13": {
"currency": "keys",
"value": 130,
"last_update": 1439447170,
"difference": 144
},
"14": {
"currency": "keys",
"value": 108,
"last_update": 1430391363,
"difference": 162,
"value_high": 114
},
"15": {
"currency": "keys",
"value": 40,
"last_update": 1433252039,
"difference": -95
},
"16": {
"currency": "keys",
"value": 44,
"last_update": 1437784980,
"difference": 39.425,
"value_high": 54
},
"17": {
"currency": "keys",
"value": 90,
"last_update": 1434161864,
"difference": 438.265,
"value_high": 100
},
"18": {
"currency": "keys",
"value": 28,
"last_update": 1433606334,
"difference": -114.33
},
"19": {
"currency": "keys",
"value": 52,
"last_update": 1421180874,
"difference": -69.11625
},
"29": {
"currency": "keys",
"value": 24,
"last_update": 1429293873,
"difference": -128.7375,
"value_high": 29
},
"30": {
"currency": "keys",
"value": 30,
"last_update": 1437869667,
"difference": -39.425,
"value_high": 40
},
"31": {
"currency": "keys",
"value": 18,
"last_update": 1402168429,
"difference": -7.0224375,
"value_high": 21
},
"32": {
"currency": "keys",
"value": 19,
"last_update": 1431897611,
"difference": -38,
"value_high": 21
},
"33": {
"currency": "keys",
"value": 16,
"last_update": 1438978898,
"difference": 56,
"value_high": 21
},
"34": {
"currency": "keys",
"value": 14,
"last_update": 1432783358,
"difference": -57,
"value_high": 19
},
"35": {
"currency": "keys",
"value": 20,
"last_update": 1439667519,
"difference": -48,
"value_high": 23
},
"36": {
"currency": "keys",
"value": 16,
"last_update": 1416523879,
"difference": -24.8994375,
"value_high": 19
},
"37": {
"currency": "usd",
"value": 165.31,
"value_high": 188.92,
"last_update": 1364519581,
"difference": -26.349
},
"38": {
"currency": "keys",
"value": 110,
"last_update": 1438115127,
"difference": 86.735,
"value_high": 115
},
"39": {
"currency": "keys",
"value": 31,
"last_update": 1421127642,
"difference": -47.723125,
"value_high": 43
},
"40": {
"currency": "keys",
"value": 31,
"last_update": 1426044571,
"difference": -9.2565000000002,
"value_high": 37
},
"43": {
"currency": "keys",
"value": 85,
"last_update": 1419053757,
"difference": 204.77272727273,
"value_high": 88
},
"44": {
"currency": "keys",
"value": 137,
"last_update": 1433875578,
"difference": 444.25166666667,
"value_high": 157
},
"45": {
"currency": "usd",
"value": 649.77,
"last_update": 1375636025,
"difference": 561.793
},
"46": {
"currency": "keys",
"value": 153,
"last_update": 1401752526,
"difference": -13.394387755102
},
"47": {
"currency": "keys",
"value": 79,
"last_update": 1425084672,
"difference": -315.86442857143,
"value_high": 93
},
"56": {
"currency": "keys",
"value": 22,
"last_update": 1436215169,
"difference": -179.4075,
"value_high": 26
},
"57": {
"currency": "keys",
"value": 40,
"last_update": 1422406359,
"difference": -58.086
},
"58": {
"currency": "keys",
"value": 28,
"last_update": 1417887466,
"difference": -177.24,
"value_high": 31
},
"59": {
"currency": "keys",
"value": 23,
"last_update": 1436452318,
"difference": -94.425,
"value_high": 29
},
"6": {
"currency": "keys",
"value": 22,
"last_update": 1423766468,
"difference": -28.33125,
"value_high": 25
},
"60": {
"currency": "keys",
"value": 16,
"last_update": 1431406119,
"difference": -122.395,
"value_high": 22
},
"61": {
"currency": "keys",
"value": 25,
"last_update": 1428174196,
"difference": -664.9875,
"value_high": 30
},
"62": {
"currency": "keys",
"value": 40,
"last_update": 1439097289,
"difference": -72,
"value_high": 41
},
"63": {
"currency": "keys",
"value": 70,
"last_update": 1431721568,
"difference": -247
},
"67": {
"currency": "keys",
"value": 90,
"last_update": 1402459658,
"difference": 793.5354375,
"value_high": 99
},
"68": {
"currency": "keys",
"value": 63,
"last_update": 1434103924,
"difference": -133.385
},
"69": {
"currency": "keys",
"value": 70,
"last_update": 1434472713,
"difference": 171.495,
"value_high": 90
},
"7": {
"currency": "keys",
"value": 20,
"last_update": 1419464617,
"difference": 17.24625,
"value_high": 25
},
"71": {
"currency": "keys",
"value": 90,
"last_update": 1434448204,
"difference": 171.495
},
"75": {
"currency": "usd",
"value": 217.11,
"value_high": 290.49,
"last_update": 1384832245,
"difference": 0
},
"79": {
"currency": "keys",
"value": 75,
"last_update": 1439565200,
"difference": -168
},
"8": {
"currency": "keys",
"value": 48,
"last_update": 1421187499,
"difference": -49.36875,
"value_high": 56
},
"9": {
"currency": "keys",
"value": 41,
"last_update": 1431063296,
"difference": -103.565,
"value_high": 53
},
"73": {
"currency": "keys",
"value": 169,
"last_update": 1420050012,
"difference": 278.93125,
"value_high": 195
},
"78": {
"currency": "keys",
"value": 75,
"last_update": 1423246411,
"difference": -18.8875
},
"70": {
"currency": "keys",
"value": 91,
"last_update": 1403662920,
"difference": 847.51875,
"value_high": 108
},
"65": {
"currency": "keys",
"value": 64,
"last_update": 1411708482,
"difference": 555.883
},
"83": {
"currency": "keys",
"value": 80,
"last_update": 1418974322,
"difference": 1146.25,
"value_high": 84
},
"86": {
"currency": "keys",
"value": 70,
"last_update": 1419116750,
"difference": 992.25
},
"81": {
"currency": "keys",
"value": 133,
"last_update": 1421127817,
"difference": 1861.201875,
"value_high": 150
},
"88": {
"currency": "keys",
"value": 40,
"last_update": 1434472710,
"difference": 304.88,
"value_high": 50
},
"87": {
"currency": "keys",
"value": 122,
"last_update": 1427764322,
"difference": 2325.6
},
"90": {
"currency": "keys",
"value": 62,
"last_update": 1431609884,
"difference": 1167.46
}
}
}
},
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 1.66,
"last_update": 1425877431,
"difference": -0.11
}
},
"Non-Craftable": {
"0": {
"currency": "metal",
"value": 1.55,
"last_update": 1440003716,
"difference": -0.11
}
}
}
}
}
},
"A Whiff of the Old Brimstone": {
"defindex": [
610
],
"prices": {
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 1.44,
"last_update": 1437527131,
"difference": -0.055
}
},
"Non-Craftable": {
"0": {
"currency": "metal",
"value": 0.88,
"last_update": 1438115814,
"difference": -0.12
}
}
}
}
}
},
"AWPer Hand": {
"defindex": [
851
],
"prices": {
"1": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 9.33,
"last_update": 1434818573,
"difference": -0.335,
"value_high": 10
}
}
}
},
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 2.88,
"last_update": 1438022976,
"difference": -0.06,
"value_high": 3
}
}
}
},
"11": {
"Tradable": {
"Craftable": {
"0": {
"currency": "keys",
"value": 9,
"last_update": 1439223013,
"difference": -4
}
}
}
}
}
}
}
}
}
我在从 JSON 获取值时遇到问题,该值在键名中包含 space。我正在使用 JavaScript 来获取值,但如果密钥包含 space(与死亡擦肩而过)或者它是一个数字 (6),我将无法使其工作。
有什么办法可以不编辑 JSON 文件吗?
提前致谢。 :)
有两种访问属性的方法;
foo.bar
foo['bar']
你需要使用第二个选项来使用空格,例如foo['bar bar'].baz