将字典转换为JSON数组时出现TypeError [英] TypeError when converting dictionary to JSON array

查看:103
本文介绍了将字典转换为JSON数组时出现TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用一个Python字典,其中的键和值是字符串并将其转换为JSON字符串。



这是我现在所在: p>

  import json 

def create_simple_meeting(subject,startDate,endDate,location,body):
info = dict()
如果(subject!=):
info [subject] = subject
if(startDate!=):
info [ startDate
如果(endDate!=):
info [endDate] = endDate
if(body!=):
info [身体] = body
if(location!=):
info [location] = location
print(json.dumps(dict))

create_simple_meeting(这是我们的会议的主题,2014-05-29 11:00:00,2014-05-29 12:00:00,Boca Raton,我们一定会遇到up,man)

它给我这个错误

 文件/ Users / bens / Documents / workspace /复制ws1为py java播放/ o pias / robot / libs / playing.py,第15行,在create_simple_meeting 
print(json.dumps(dict))
文件/System/Library/Frameworks/Python.framework/Versions/2.7/ lib / python2.7 / json / __ init__.py,第243行转储
返回_default_encoder.encode(obj)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/ lib / python2.7 / json / encoder.py,第207行,编码
chunks = self.iterencode(o,_one_shot = True)
文件/System/Library/Frameworks/Python.framework /Versions/2.7/lib/python2.7/json/encoder.py,第270行,iterencode
返回_iterencode(o,0)
文件/System/Library/Frameworks/Python.framework TypeVisions2.7/lib/python2.7/json/encoder.py,第184行默认
raise TypeError(repr(o)+不是JSON可序列化)
TypeError:<输入'dict'>不是JSON序列化


解决方案

您正在尝试序列化 type object, dict ,而不是 info 。转储正确的变量:

  print(json.dumps(info))
/ pre>

How do I take a python dictionary where the keys and values are Strings and convert it into a JSON String.

This is what I have right now:

import json

def create_simple_meeting(subject, startDate, endDate, location, body):
    info = dict()
    if(subject != ""):
        info["subject"] = subject
    if(startDate != ""):
        info["startDate"] = startDate
    if(endDate != ""):
        info["endDate"] = endDate
    if(body != ""):
        info["body"] = body
    if(location != ""):
        info["location"] = location
    print(json.dumps(dict))

create_simple_meeting("This is the subject of our meeting.","2014-05-29 11:00:00","2014-05-29 12:00:00", "Boca Raton", "We should definitely meet up, man")

And it gives me this error

  File "/Users/bens/Documents/workspace/Copy of ws1 for py java playing/opias/robot/libs/playing.py", line 15, in create_simple_meeting
    print(json.dumps(dict))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <type 'dict'> is not JSON serializable

解决方案

You are trying to serialise the type object, dict, instead of info. Dump the right variable:

print(json.dumps(info))

这篇关于将字典转换为JSON数组时出现TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆