首页 > 解决方案 > 如何在python中将JSON字符串转换为整数?

问题描述

如何从这个 json 将 year 和 isbn 转换为整数?

({
    "title": "The Notebook",
    "author": "Nicholas Sparks",
    "year": "1996",
    "isbn": "0553816713"
})

标签: pythonjson

解决方案


您可以简单地使用相应的 int 值更新这些值

data = {
    "title": "The Notebook",
    "author": "Nicholas Sparks",
    "year": "1996",
    "isbn": "0553816713"
    }

data["year"] = int(data["year"])
data["isbn"] = int(data["isbn"])

print(data)

OUT: {'title': 'The Notebook', 'author': 'Nicholas Sparks', 'year': 1996, 'isbn': 553816713}

推荐阅读