【问题标题】:JSON is not serializableJSON 不可序列化
【发布时间】:2015-04-10 21:43:42
【问题描述】:

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

我的错误是; TypeError: is not JSON serializable

我的 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": ""
    }
}

【问题讨论】:

  • 你遇到了什么错误?
  • 你为什么要导入json三次??
  • @ForceBru:确定一下?
  • 你永远不会有足够的jsons
  • @runDOSrun,是的。我们需要更多 json的!

标签: python json serialization


【解决方案1】:

在我看来,您需要 json.load,而不是 json.dumps,才能从文件中读取 json 结构。

data = json.load(f)

另外,多投几个 import jsons 可能会有所帮助;)

【讨论】:

    猜你喜欢
    • 2013-04-26
    • 1970-01-01
    • 2015-01-07
    • 2019-07-05
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2018-09-12
    相关资源
    最近更新 更多