在Python服务器上处理JSON对象会导致“list”对象没有属性“read”

2024-10-01 09:20:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有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对象的代码块之一)

^{pr2}$

这是获取的-“list”对象没有属性“read”-错误

删除的\u个朋友=json.load文件(self.request.获取全部(“a”)声明。在

由于我是python和javascript的新手,而且是自学成才的,我确信我在做一些概念上错误的事情。感谢任何帮助。在


Tags: 对象textjsonnewforiftitlevalue
3条回答

get_all函数返回查询字符串参数a的所有值。因为您只有一个a,所以应该使用get。另外,使用json.loads将字符串解析为json。在

removed_friends = json.loads(self.request.get("a"))
#                     here ^       and here ^

您给json.load一个导致此错误的列表。在

此外,使用json.loads代替json.load。在

试试这个:

info = json.dumps(self.request.get_all("a"))
removed_friends = json.loads(info)

根据文件:

load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw) Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object.

所以json.load文件应为类似文件的对象,而不是list,而request.get_all返回该对象。在

相关问题 更多 >