在Python服务器处理JSON对象时出现'list'对象没有'read'属性错误
我有一段JavaScript代码,它从某个地方获取数据,并创建一个JSON对象,然后把这个对象发送到服务器。
for (i=0;i<selected.length;i++)
{
if (selected[i].value == "available")
{
//Add item to the selectedjsonObj list
//alert("The new select ID is: " + selected[i].text);
selectedjsonObj[selected[i].text] = selected[i].text;
selected[i].value = "selected";
selectedChange++;
}
}
for (i=0;i<available.length;i++)
{
if (available[i].value == "selected")
{
//Add item to the availablejsonObj list
//alert("The new available ID is: " + available[i].text);
availablejsonObj[available[i].text] = available[i].text;
available[i].value = "available";
availableChange++;
}
}
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("title_msg").innerHTML=xmlhttp.responseText;
// var el = document.getElementById('userid_msg');
if (xmlhttp.responseText=="Updated") {
document.getElementById("title_msg").innerHTML=xmlhttp.responseText;
}
else {
alert("An error occurred saving your changes. Please refresh the page and try again.");
document.getElementById("title_msg").innerHTML=xmlhttp.responseText;
}
}
}
var x=document.forms["cohort"]["title"].value;
if (selectedChange && availableChange) {
xmlhttp.open("GET","/update/cohortfriends?a=" + JSON.stringify(availablejsonObj) + "&s=" + JSON.stringify(selectedjsonObj) + "&t=" + x, true);
xmlhttp.send();
}
if (selectedChange) {
xmlhttp.open("GET","/update/cohortfriends?s=" + JSON.stringify(selectedjsonObj) + "&t=" + x, true);
xmlhttp.send();
}
if (availableChange) {
xmlhttp.open("GET","/update/cohortfriends?a=" + JSON.stringify(availablejsonObj) + "&t=" + x, true);
xmlhttp.send();
}
document.getElementById("save_cohort_friends").innerHTML="No Changes";
在服务器上,我尝试像下面这样读取这个对象(这里只展示了处理JSON对象的其中一部分代码):
removed_friends = json.load(self.request.get_all("a"))
for key, value in removed_friends.iteritems():
logging.debug("Processing the Removed_Friend Value: %s" % value)
logging.debug("With the Key of: %s" % key)
Cohorts.remove_friend_from_Cohort(my_lhcohort, User.by_id_name(value), cohort_key)
在执行这段代码时,我遇到了一个错误,提示说'a - 'list'对象没有'read'属性',这个错误出现在
removed_friends = json.load(self.request.get_all("a")) 这行代码上。
因为我对Python和JavaScript都很陌生,而且是自学的,所以我知道我在某个概念上搞错了。任何帮助都非常感谢。
3 个回答
0
来自文档的内容:
load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw) 将
fp
(一个支持.read()
的类似文件的对象,里面包含一个 JSON 文档)反序列化为 Python 对象。
所以,json.load 期待的是一个类似文件的对象,而不是一个 list
,而 request.get_all
返回的就是一个列表。
0
你给了一个列表给 json.load
,这就导致了这个错误。
而且,应该用 json.loads
来替代 json.load
。
试试这个:
info = json.dumps(self.request.get_all("a"))
removed_friends = json.loads(info)
0
get_all
这个函数是用来获取查询字符串参数的所有值的。不过因为你这里只有一个,所以应该用get
来代替。此外,还可以使用json.loads
把一个字符串解析成json格式。
removed_friends = json.loads(self.request.get("a"))
# here ^ and here ^