发生其他错误:应为值:第1行第1列(字符0)#1

2024-09-24 22:31:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我写的API有问题。我的Json有一个错误,我觉得很奇怪。总体思路是搜索一组VIN并将其存储到Json文件中。代码工作到一个点,直到我存储第一个VIN,然后在第二个VIN上工作,但是我在标题中得到了错误。我知道我做错了什么,似乎问题在于保存数据。请在下面查找代码

api.py

import requests
import json
from requests.exceptions import HTTPError
#Define The VINs Lists
VINs = ['KPTGOC1FSCP300549', '5J6RM3H53CL015747', 'TDSY61U032248', 'JDAJ210GOO1052908', 'KNAKH812BA7683150', 'jtmbh31v806052793', 'Kmhsh81gdcu864966' ]

try:
    with open('vins.json', 'w') as f:
        for vin in VINs:
            response = requests.get(f'https://vindecodervehicle.com/api/v1/?id=xxxxxxx&key=xxxxxxx&vin={vin}')
            response.raise_for_status()
            # access JSOn content
            jsonResponse = response.json()
            #Create the json File.
            json.dump(jsonResponse, f, indent=4)
            print("Entire JSON response")
            print(jsonResponse)

except HTTPError as http_err:
    print(f'HTTP error occurred: {http_err}')
except Exception as err:
    print(f'Other error occurred: {err}')

正在存储的VIN文件。 vins.json

{
    "data": {
        "matchingManufacturers": {
            "array": [
                {
                    "manuId": 175,
                    "manuName": "SSANGYONG"
                }
            ]
        },
        "matchingModels": {
            "array": [
                {
                    "manuId": 175,
                    "modelId": 5000,
                    "modelName": "REXTON / REXTON II (GAB_)"
                }
            ]
        },
        "matchingVehicles": {
            "array": [
                {
                    "manuId": 175,
                    "modelId": 5000,
                    "carId": 18259,
                    "vehicleTypeDescription": "2.7 Xdi",
                    "carName": "SSANGYONG REXTON / REXTON II (GAB_) 2.7 Xdi"
                },
                {
                    "manuId": 175,
                    "modelId": 5000,
                    "carId": 27999,
                    "vehicleTypeDescription": "2.7 D 4x4",
                    "carName": "SSANGYONG REXTON / REXTON II (GAB_) 2.7 D 4x4"
                },
                {
                    "manuId": 175,
                    "modelId": 5000,
                    "carId": 28000,
                    "vehicleTypeDescription": "2.7 Xdi",
                    "carName": "SSANGYONG REXTON / REXTON II (GAB_) 2.7 Xdi"
                }
            ]
        },
        "matchingVehiclesCount": 3,
        "dataSource": [
            {
                "dataSourceKey": "vin_filter"
            }
        ]
    },
    "status": 200
} {
    "data": {
        "matchingManufacturers": {
            "array": [
                {
                    "manuId": 45,
                    "manuName": "HONDA"
                }
            ]
        },
        "matchingModels": {
            "array": [
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "modelName": "CR-V IV (RM_)"
                }
            ]
        },
        "matchingVehicles": {
            "array": [
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 6978,
                    "vehicleTypeDescription": "2.0",
                    "carName": "HONDA CR-V IV (RM_) 2.0"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 6980,
                    "vehicleTypeDescription": "2.4",
                    "carName": "HONDA CR-V IV (RM_) 2.4"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 7980,
                    "vehicleTypeDescription": "2.4 AWD",
                    "carName": "HONDA CR-V IV (RM_) 2.4 AWD"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 55240,
                    "vehicleTypeDescription": "2.4 AWD",
                    "carName": "HONDA CR-V IV (RM_) 2.4 AWD"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 56243,
                    "vehicleTypeDescription": "2.0 iVtec (RE5, RM1)",
                    "carName": "HONDA CR-V IV (RM_) 2.0 iVtec (RE5, RM1)"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 56244,
                    "vehicleTypeDescription": "2.2 i-DTEC AWD (RE6)",
                    "carName": "HONDA CR-V IV (RM_) 2.2 i-DTEC AWD (RE6)"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 56477,
                    "vehicleTypeDescription": "2.4 4WD",
                    "carName": "HONDA CR-V IV (RM_) 2.4 4WD"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 56765,
                    "vehicleTypeDescription": "2.0 AWD (RE5, RM2)",
                    "carName": "HONDA CR-V IV (RM_) 2.0 AWD (RE5, RM2)"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 109764,
                    "vehicleTypeDescription": "2.4 (RM3)",
                    "carName": "HONDA CR-V IV (RM_) 2.4 (RM3)"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 112198,
                    "vehicleTypeDescription": "2.4",
                    "carName": "HONDA CR-V IV (RM_) 2.4"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 112199,
                    "vehicleTypeDescription": "2.4 4WD",
                    "carName": "HONDA CR-V IV (RM_) 2.4 4WD"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 122300,
                    "vehicleTypeDescription": "2.4 4WD",
                    "carName": "HONDA CR-V IV (RM_) 2.4 4WD"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 125363,
                    "vehicleTypeDescription": "2.4 i-VTEC (RM4)",
                    "carName": "HONDA CR-V IV (RM_) 2.4 i-VTEC (RM4)"
                },
                {
                    "manuId": 45,
                    "modelId": 10180,
                    "carId": 125364,
                    "vehicleTypeDescription": "2.4 i-VTEC 4WD (RM4)",
                    "carName": "HONDA CR-V IV (RM_) 2.4 i-VTEC 4WD (RM4)"
                }
            ]
        },
        "matchingVehiclesCount": 14,
        "dataSource": [
            {
                "dataSourceKey": "vin_filter"
            }
        ]
    },
    "status": 200
}

Tags: rmjsonarrayawdcrvinivcarid
3条回答

response.json()返回dict而不是json,因此之后不需要json.dump

下面是您需要更改的部分代码

jsonResponse = response.json()
#Create the json File.
print("Entire JSON response")
print(jsonResponse)

不要在打开的文件中多次转储。相反,您可以创建一个列表,将所有http_响应添加到该列表中,并一次性转储它

当您多次转储时,只需将多个字典(json记录)插入json文件中,而不在它们之间插入“,”并破坏语法

try:
    with open('vins.json', 'w') as f:
       http_responses = []
       for vin in VINs:
           response = requests.get(f'https://vindecodervehicle.com/api/v1/?id=xxxxxxx&key=xxxxxxx&vin={vin}')
            response.raise_for_status()
            # access JSOn content
            jsonResponse = response.json()
            http_responses.append(jsonResponse)
            print("Entire JSON response")
            print(jsonResponse)
       #Create the json File.
       json.dump(jsonResponse, http_responses, indent=4)

except HTTPError as http_err:
    print(f'HTTP error occurred: {http_err}')
except Exception as err:
    print(f'Other error occurred: {err}')

我认为您不应该尝试直接更新到文件的根目录,而是应该加载文件并更新要添加数据的特定键

import requests
import json
from requests.exceptions import HTTPError
#Define The VINs Lists
VINs = ['KPTGOC1FSCP300549', '5J6RM3H53CL015747', 'TDSY61U032248', 'JDAJ210GOO1052908', 'KNAKH812BA7683150', 'jtmbh31v806052793', 'Kmhsh81gdcu864966' ]

try:
    with open('vins.json', 'w') as f:
        my_data = json.load(f)

        for vin in VINs:
            response = requests.get(f'https://vindecodervehicle.com/api/v1/?id=xxxxxxx&key=xxxxxxx&vin={vin}')
            response.raise_for_status()
            # access JSOn content
            jsonResponse = response.json()
            my_data["data"].update(jsonResponse["data"]) 


            #Create the json File.
            json.dump(my_data, f, indent=4)
            print("Entire JSON response")
            print(jsonResponse)
 
except HTTPError as http_err:
    print(f'HTTP error occurred: {http_err}')
except Exception as err:
    print(f'Other error occurred: {err}')

相关问题 更多 >