JSON不可序列化

时间:2015-02-10 18:12:14

标签: python json serialization

我正在使用Web服务和python请求模块。我收到错误,我的文件不可序列化。

我的错误是; TypeError:不是JSON可序列化的

我的json文件是本地文件,其中包含我请求的数据。

import requests

from json import JSONEncoder
import json


f = open('C:\Users\Administrator\Desktop\myla311.json', 'r')


url = "myURL"
headers = {'Content-type': 'raw', 'Accept': '/'}

response = requests.post(url, data=json.load(f), headers=headers)

f.close()

print response.text


print response

我的预期输出

 {
    "MetaData": {},
    "RequestSpecificDetail": {
        "ParentSRNumberForLink": ""
    },
    "SRData": {
        "Anonymous": "Y",
        "Assignee": "",
        "CreatedByUserLogin": "",
        "CustomerAccessNumber": "",
        "LADWPAccountNo": "",
        "Language": "English",
        "ListOfLa311GisLayer": {},
        "ListOfLa311ServiceRequestNotes": {
            "La311ServiceRequestNotes": [
                {
                    "Comment": "hxhdudi",
                    "CommentType": "Feedback",
                    "FeedbackSRType": "Weed Abatement for Pvt Parcels",
                    "IsSrNoAvailable": "N"
                },
                {
                    "Comment": "",
                    "CommentType": "External",
                    "CreatedByUser": "",
                    "IsSrNoAvailable": "N"
                }
            ]
        },
        "LoginUser": "",
        "MobilOS": "Android",
        "NewContactEmail": "",
        "NewContactFirstName": "",
        "NewContactLastName": "",
        "NewContactPhone": "",
        "Owner": "Other",
        "ParentSRNumber": "",
        "Priority": "Normal",
        "SRCommunityPoliceStation": "RAMPART",
        "SRType": "Feedback",
        "ServiceDate": "01/22/2015",
        "Source": "Mobile App",
        "Status": "Open",
        "UpdatedByUserLogin": ""
    }
}

1 个答案:

答案 0 :(得分:1)

在我看来,为了从文件中读取json结构,你需要json.load,而不是json.dumps

data = json.load(f)

此外,它可能有助于再投入一些import json s;)