如何从json文件显示所需键的属性

时间:2018-06-04 01:33:35

标签: jquery json

我收到了以下json文件

    self.text_edit = QtWidgets.QTextEdit()
    self.text_edit.textChanged.connect(self.add_indent)

def add_indent(self):
    if self.text_edit.toPlainText().endswith(':\n'):
        self.text_edit.insertPlainText('    ')

我想使用以下功能:

{
"USD": {
        "symbol": "$",
        "full_name": "US Dollar",
        "code": "USD",
    },
"JPY": {
        "symbol": "¥",
        "full_name": "Japanese Yen",
        "code": "JPY",
    },    
}

如何继续阅读所需的密钥以获取这些属性?

目前,我加载了我的json文件

myfunction("jpy", "full_name") = "Japanese Yen "
myfunction("usd", "symbol") = "$"

我的对象不会获得更深层次的嵌套属性。第一个参数只是属性名称的小写版本

1 个答案:

答案 0 :(得分:0)

您可以使用array来存储索引,例如:



var text = '{"USD": [{"symbol": "$","full_name": "US Dollar","code": "USD"}],"JPY": [{"symbol": "¥","full_name": "Japanese Yen","code": "JPY"}]}';

json_data = JSON.parse(text);

document.write(get_data(json_data,['JPY','full_name']));

function get_data(json,arr){
    return json[arr[0]][0][arr[1]];
}