简体   繁体   English

在使用 python “字符串索引必须是整数”的 JSON 导入中出现错误

[英]Getting an error in JSON Import with python “string indices must be integers”

import json

json_dump = json.dumps('1.json')

json_object = json.loads(json_dump)

print(json_object["client_id"])

I am writing a code in python which extracts email id from a json file.我正在 python 中编写代码,该代码从 json 文件中提取 email id。 But I am getting an error:-但我收到一个错误:-

" string indices must be integers " " 字符串索引必须是整数 "

Here is the JSON FIle i am importing:-这是我正在导入的 JSON 文件:-

  {
  "type": "Some Data",
  "project_id": "Some Data",
  "private_key_id": "Some Data",
  "private_key": "Some Data",
  "client_email": "Some Data",
  "client_id": "Some Data",
  "auth_uri": "Some Data",
  "token_uri": "Some Data",
  "auth_provider_x509_cert_url": "Some Data",
  "client_x509_cert_url": "Some Data"
  }

You should first open the file, and pass the opened file in json.load(file)您应该首先打开文件,并将打开的文件传递给json.load(file)

import json
# opening the json file
json_file = open('1.json','r+')

json_object = json.load(json_file)

print(json_object["client_id"])


Output:
Some Data

for looping over json files named as numbers用于循环命名为数字的 json 文件

import json
# opening the json file
# files named as 1.json to 3.json will do this
for i in range(1,4):
    json_file = open(str(i)+'.json','r+')

    json_object = json.load(json_file)

    print(json_object["client_id"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在python中解析json时,我不断收到以下错误:“字符串索引必须为整数” - I keep getting the following error trying to parse json in python “string indices must be integers” 在 python 中使用 for 循环时,字符串索引必须是整数 - Getting error as string indices must be integers while using for loop in python 在Python中抓取json时出现“字符串索引必须为整数”错误 - “string indices must be integers” error when scraping json in Python Python 和 JSON 错误 - TypeError:字符串索引必须是整数 - Python and JSON Error - TypeError: string indices must be integers python和json,错误-TypeError:字符串索引必须为整数 - python and json, Error - TypeError: string indices must be integers 类型错误:使用 Python 解析 Json 时,字符串索引必须是整数错误 - TypeError: string indices must be integers error when parsing Json with Python Python:JSON标准化“字符串索引必须为整数”错误 - Python: json normalize “String indices must be integers” error Python for loop json.loads错误字符串索引必须为整数 - Python for loop json.loads error string indices must be integers Python Json TypeError:字符串索引必须是整数错误 - Python Json TypeError: string indices must be integers error python,Json和字符串索引必须是整数,而不是str - python, Json and string indices must be integers, not str
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM