I am having trouble using json.loads to convert to a dict object and I can't figure out what I'm doing wrong.The exact error I get running this is
我在使用json时遇到了麻烦。要转换成命令对象,我不知道我做错了什么。我运行的错误是
ValueError: Expecting property name: line 1 column 2 (char 1)
Here is my code:
这是我的代码:
__author__ = 'xxdpavelxx'
from kafka.client import KafkaClient
from kafka.consumer import SimpleConsumer
from kafka.producer import SimpleProducer, KeyedProducer
import pymongo
from pymongo import MongoClient
import json
c = MongoClient("54.210.157.57")
db = c.test_database3
collection = db.tweet_col
kafka = KafkaClient("54.210.157.57:9092")
consumer = SimpleConsumer(kafka,"myconsumer","test")
for tweet in consumer:
print tweet.message.value
jsonTweet=json.loads(({u'favorited': False, u'contributors': None})
collection.insert(jsonTweet)
I'm pretty sure that the error is occuring at the 2nd to last line
我很确定错误发生在倒数第二行
jsonTweet=json.loads({u'favorited': False, u'contributors': None})
but I do not know what to do to fix it. Any advice would be appreciated.
但我不知道该怎么修复它。如有任何建议,我们将不胜感激。
5 个解决方案
#1
53
json.loads
will load a json string into a python dict
, json.dumps
will dump a python dict
to a json string, for example:
json。load将把json字符串加载到python词典json中。转储将把python命令转储到json字符串中,例如:
>>> json_string = '{"favorited": false, "contributors": null}'
'{"favorited": false, "contributors": null}'
>>> value = json.loads(json_string)
{u'favorited': False, u'contributors': None}
>>> json_dump = json.dumps(value)
'{"favorited": false, "contributors": null}'
So that line is incorrect since you are trying to load
a python dict
, and json.loads
is expecting a valid json string
which should have <type 'str'>
.
因此,这一行是不正确的,因为您正在尝试加载python命令和json。load期望有一个有效的json字符串,该字符串应该具有
So if you are trying to load the json, you should change what you are loading to look like the json_string
above, or you should be dumping it. This is just my best guess from the given information. What is it that you are trying to accomplish?
所以,如果你想加载json,你应该改变你正在加载的内容,使其看起来像上面的json_string,或者你应该将它转储。这只是我对给定信息的最好猜测。你想要完成的是什么?
Also you don't need to specify the u
before your strings, as @Cld mentioned in the comments.
您也不需要在字符串之前指定u,如注释中提到的@Cld。
#2
86
I encountered another problem which return same error.
我遇到了另一个同样错误的问题。
Single quote issue
I use a json string with single quotes :
我使用带有单引号的json字符串:
{
'property': 1
}
But json.loads accepts only double quotes for json properties :
但json。load只接受json属性的双引号:
{
"property": 1
}
Final coma issue
json.loads doesn't accept a final coma like :
json。load不接受最后的昏迷,比如:
{
"property": "text",
"property2": "text2",
}
Solution : ast to solve single quote and final coma issues
You can use ast (available in standard library Python 2 and 3) package for this processing. Here is an example :
您可以使用ast(标准库Python 2和3中提供的)包进行此处理。这里有一个例子:
import ast
# ast.literal_eval() return a dict object, we must use json.dumps to get JSON string
import json
# Single quote to double with ast.literal_eval()
json_data = "{'property': 'text'}"
json_data = ast.literal_eval(json_data)
print(json.dumps(json_data))
# Displays : {"property": "text"}
# ast.literal_eval() with double quotes
json_data = '{"property": "text"}'
json_data = ast.literal_eval(json_data)
print(json.dumps(json_data))
# Displays : {"property": "text"}
# ast.literal_eval() with final coma
json_data = "{'property': 'text', 'property2': 'text2',}"
json_data = ast.literal_eval(json_data)
print(json.dumps(json_data))
# Displays : {"property2": "text2", "property": "text"}
Use ast will prevent you to single quote and final coma issues.
使用ast将防止您的单引号和最终昏迷问题。
json.dumps with single quotes
In order to use json.dumps with simple quotes easily you can use this code :
为了使用json。使用简单引号的转储可以轻松使用以下代码:
import ast
import json
data = json.dumps(ast.literal_eval(json_data_single_quote))
ast documentations
ast Python 3医生
ast Python 2文档
I hope it will help
我希望它能有所帮助。
#3
5
- replace all single quotes with double quotes
- 用双引号替换所有单引号
-
replace 'u"' from your strings to '"' ... so basically convert internal unicodes to strings before loading the string into json
将“u”从字符串中替换为“”……在将字符串加载到json之前,先将内部的unicodes转换为字符串
>> strs = "{u'key':u'val'}"
> > str = " { u 'key”:u 'val“}”
>> strs = strs.replace("'",'"')
> > str = strs.replace(“”,“”)
>> json.loads(strs.replace('u"',''))
> > json.loads(strs.replace(“u””,“))
#4
1
All other answers may answer your query, but I faced same issue which was due to stray ,
which I added at the end of my json string like this:
所有其他的答案都可以回答您的查询,但是我遇到了同样的问题,因为我在json字符串的末尾添加了这样一个问题:
{
"key":"123sdf",
"bus_number":"asd234sdf",
}
I finally got it working when I removed extra ,
like this:
当我移除多余的部分时,我终于让它工作了,就像这样:
{
"key":"123sdf",
"bus_number":"asd234sdf"
}
Hope this help! cheers.
希望这次的帮助!欢呼。
#5
0
used ast, example
使用ast,例子
In [15]: a = "[{'start_city': '1', 'end_city': 'aaa', 'number': 1},\
...: {'start_city': '2', 'end_city': 'bbb', 'number': 1},\
...: {'start_city': '3', 'end_city': 'ccc', 'number': 1}]"
In [16]: import ast
In [17]: ast.literal_eval(a)
Out[17]:
[{'end_city': 'aaa', 'number': 1, 'start_city': '1'},
{'end_city': 'bbb', 'number': 1, 'start_city': '2'},
{'end_city': 'ccc', 'number': 1, 'start_city': '3'}]
#1
53
json.loads
will load a json string into a python dict
, json.dumps
will dump a python dict
to a json string, for example:
json。load将把json字符串加载到python词典json中。转储将把python命令转储到json字符串中,例如:
>>> json_string = '{"favorited": false, "contributors": null}'
'{"favorited": false, "contributors": null}'
>>> value = json.loads(json_string)
{u'favorited': False, u'contributors': None}
>>> json_dump = json.dumps(value)
'{"favorited": false, "contributors": null}'
So that line is incorrect since you are trying to load
a python dict
, and json.loads
is expecting a valid json string
which should have <type 'str'>
.
因此,这一行是不正确的,因为您正在尝试加载python命令和json。load期望有一个有效的json字符串,该字符串应该具有
So if you are trying to load the json, you should change what you are loading to look like the json_string
above, or you should be dumping it. This is just my best guess from the given information. What is it that you are trying to accomplish?
所以,如果你想加载json,你应该改变你正在加载的内容,使其看起来像上面的json_string,或者你应该将它转储。这只是我对给定信息的最好猜测。你想要完成的是什么?
Also you don't need to specify the u
before your strings, as @Cld mentioned in the comments.
您也不需要在字符串之前指定u,如注释中提到的@Cld。
#2
86
I encountered another problem which return same error.
我遇到了另一个同样错误的问题。
Single quote issue
I use a json string with single quotes :
我使用带有单引号的json字符串:
{
'property': 1
}
But json.loads accepts only double quotes for json properties :
但json。load只接受json属性的双引号:
{
"property": 1
}
Final coma issue
json.loads doesn't accept a final coma like :
json。load不接受最后的昏迷,比如:
{
"property": "text",
"property2": "text2",
}
Solution : ast to solve single quote and final coma issues
You can use ast (available in standard library Python 2 and 3) package for this processing. Here is an example :
您可以使用ast(标准库Python 2和3中提供的)包进行此处理。这里有一个例子:
import ast
# ast.literal_eval() return a dict object, we must use json.dumps to get JSON string
import json
# Single quote to double with ast.literal_eval()
json_data = "{'property': 'text'}"
json_data = ast.literal_eval(json_data)
print(json.dumps(json_data))
# Displays : {"property": "text"}
# ast.literal_eval() with double quotes
json_data = '{"property": "text"}'
json_data = ast.literal_eval(json_data)
print(json.dumps(json_data))
# Displays : {"property": "text"}
# ast.literal_eval() with final coma
json_data = "{'property': 'text', 'property2': 'text2',}"
json_data = ast.literal_eval(json_data)
print(json.dumps(json_data))
# Displays : {"property2": "text2", "property": "text"}
Use ast will prevent you to single quote and final coma issues.
使用ast将防止您的单引号和最终昏迷问题。
json.dumps with single quotes
In order to use json.dumps with simple quotes easily you can use this code :
为了使用json。使用简单引号的转储可以轻松使用以下代码:
import ast
import json
data = json.dumps(ast.literal_eval(json_data_single_quote))
ast documentations
ast Python 3医生
ast Python 2文档
I hope it will help
我希望它能有所帮助。
#3
5
- replace all single quotes with double quotes
- 用双引号替换所有单引号
-
replace 'u"' from your strings to '"' ... so basically convert internal unicodes to strings before loading the string into json
将“u”从字符串中替换为“”……在将字符串加载到json之前,先将内部的unicodes转换为字符串
>> strs = "{u'key':u'val'}"
> > str = " { u 'key”:u 'val“}”
>> strs = strs.replace("'",'"')
> > str = strs.replace(“”,“”)
>> json.loads(strs.replace('u"',''))
> > json.loads(strs.replace(“u””,“))
#4
1
All other answers may answer your query, but I faced same issue which was due to stray ,
which I added at the end of my json string like this:
所有其他的答案都可以回答您的查询,但是我遇到了同样的问题,因为我在json字符串的末尾添加了这样一个问题:
{
"key":"123sdf",
"bus_number":"asd234sdf",
}
I finally got it working when I removed extra ,
like this:
当我移除多余的部分时,我终于让它工作了,就像这样:
{
"key":"123sdf",
"bus_number":"asd234sdf"
}
Hope this help! cheers.
希望这次的帮助!欢呼。
#5
0
used ast, example
使用ast,例子
In [15]: a = "[{'start_city': '1', 'end_city': 'aaa', 'number': 1},\
...: {'start_city': '2', 'end_city': 'bbb', 'number': 1},\
...: {'start_city': '3', 'end_city': 'ccc', 'number': 1}]"
In [16]: import ast
In [17]: ast.literal_eval(a)
Out[17]:
[{'end_city': 'aaa', 'number': 1, 'start_city': '1'},
{'end_city': 'bbb', 'number': 1, 'start_city': '2'},
{'end_city': 'ccc', 'number': 1, 'start_city': '3'}]