当前位置: 首页 > 面试题库 >

从JSON获取价值,JSON使用JavaScript在键名中具有空格

宇文峰
2023-03-14
问题内容
{
"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获取值时遇到问题,该值在键名中包含空格。我使用JavaScript来获取值,但是如果键包含空格(带有死亡的画笔)或它是数字(6),则无法使它起作用。

没有编辑JSON文件,有什么办法吗?

提前致谢。:)


问题答案:

有两种访问属性的方法:

  1. foo.bar
  2. foo['bar']

您需要使用第二个选项来使用空格,例如 foo['bar bar'].baz



 类似资料:
  • 问题内容: 假设我有这个JSON: 我正在尝试通过PHP: 我正在尝试获取成就的第一个“ id” 。 我该如何解决? 问题答案: 当您设置的第二个参数来,它会返回一个数组。 返回一个数组。

  • 问题内容: 我正在连接到第三方API,并获取了很长的JSON字符串。我只需要一个值,但是它位于层次结构的深处。有没有一种简单的方法就可以完成整个过程?我环顾四周,但似乎没有轻松的事情。 这是我的示例: 我一直在尝试使用Gson,以便可以将此斑点作为JsonObject。我确定有一些简单的东西,像这样: 或至少是这样的: 但是它似乎不存在。 那么,有没有可以让我做到这一点的解析器? 问题答案: 这是

  • 在的。NET框架4. x,我可以使用在中获取,然后我可以通过 但是在。Net内核,我必须注入,并且不能通过 有没有一种方法可以直接通过获取的值?

  • 如何获取密钥名称,如下所示:

  • 问题内容: 我正在返回一个json,如下所示 我正在尝试获取每个元素的键和值: 我正在戒备。为什么?我尝试过,我尝试循环但没有成功。 问题答案: 检查这个jsfiddle 从 jQuery 3.0开始 ,不推荐使用$ .parseJSON。要解析JSON字符串,请改用本机JSON.parse方法。 来源:http://api.jquery.com/jquery.parsejson/

  • 问题内容: 上面的命令仅输出以下值: 我如何像下面这样获取键名: 问题答案: 您可以使用: 更新: 要使用这些键创建一个BASH数组: 使用BASH 4+: 在较旧的BASH上,您可以执行以下操作: 然后打印: