python json.load与json.loads区别

文章目录

    • json.loads 与json.load区别
      • json.load示例
      • json.loads示例

json.loads 与json.load区别

stackoverflow上的一个提问:

In Python, what is the difference between json.load() and json.loads()?

I guess that the load() function must be used with a file object (I need thus to use a context manager) while the loads() function take the path to the file as a string. It is a bit confusing.

Does the letter “s” in json.loads() stand for string?

Thanks a lot for your answers!


Yes, s stands for string. The json.loads function does not take the file path, but the file contents as a string.


总结:

  • json.loads()参数是字符串。
  • json.load()参数是文件对象。

json.load示例

import json

f = open("1.json")
js = json.load(f)
for i in js["results"]:
    print(i["trackName"])

json.loads示例

import json

req = requests.get("https://xxx.com/ads")

js = json.loads(req.content)

你可能感兴趣的:(python,json)