Python - 创建JSON数组?并传递给ajax

时间:2018-03-27 14:08:13

标签: python arrays json ajax parsing

在我的python服务器端脚本中,我有以下

....


data = {}
data['id'] = id
data['organizer'] = organizer
data['eventStart'] = eventStartLocal
data['eventEnd'] = eventEndLocal
data['subject'] = subject
data['attendees'] = attendees
# Serializing Data 
json_data = json.dumps(data)
id += 1
print(json_data) #this is what the script returns 

在我的前端使用jquery / javascript我有这个..

$.ajax({
       type: 'REQUEST',
       dataType: 'text',
          url: 'json.py',
          success: function(data){
               console.log(data);
               obj = JSON.parse(data);
               },

            }).done(function(){

                console.log(obj)
            })

        });

我的输出是:

{"id": 0, "organizer": "Some Name", "eventStart": "09:30 AM", "eventEnd": "10:00 AM", "subject": "rental procedure", "attendees": "Some Name<br />Person 2<br />Person 3"}
{"id": 1, "organizer": "Some Name", "eventStart": "09:30 AM", "eventEnd": "10:00 AM", "subject": "rental procedure", "attendees": "Some Name<br />Person 2<br />Person 3"}
{"id": 2, "organizer": "Some Name", "eventStart": "09:30 AM", "eventEnd": "10:00 AM", "subject": "rental procedure", "attendees": "Some Name<br />Person 2<br />Person 3"}

我的问题

当我尝试在javascript中解析json时,我得到一个关于

的错误代码
  

意外的令牌{位于JSON的位置

在做了一些研究之后,我发现我必须发送一个json数组作为响应,而不是为每个事件打印jsoned行,否则我的javascript将无法解析它并将其放在一个对象中。

我的目标

从服务器获取数据并将其作为对象传递给javascript,以便我可以遍历该对象并为每个事件创建一个HTML元素。

我是这个JSON的新手,不知怎的,我需要做以下事情,但我似乎不知道如何创建一个json数组(在每个json /的末尾都有[]和线。

[
    {"id": 0, "organizer": "Some Name", "eventStart": "09:30 AM", "eventEnd": "10:00 AM", "subject": "rental procedure", "attendees": "Some Name<br />Person 2<br />Person 3"},
    {"id": 1, "organizer": "Some Name", "eventStart": "09:30 AM", "eventEnd": "10:00 AM", "subject": "rental procedure", "attendees": "Some Name<br />Person 2<br />Person 3"},
    {"id": 2, "organizer": "Some Name", "eventStart": "09:30 AM", "eventEnd": "10:00 AM", "subject": "rental procedure", "attendees": "Some Name<br />Person 2<br />Person 3"}
]

2 个答案:

答案 0 :(得分:1)

...答案结尾处的JSON数组不正确,但要生成数组,只需在Python中为json.dumps提供一个列表。像json_data_list = []; ... ; json_data_list.append(json_data); ... print(json.dumps(json_data_list)); ...

这样的东西

答案 1 :(得分:1)

您的JSON文件不正确。通常,您必须具有以下结构:

{
    "key1": [
        {
            "id": "blabla",
            "name": "Toto"
        },
        {
            "id": "blibli",
            "name": "Tata"
        }
    ],
    "key2": {
        "id": "value"
    },
    "key3": "value"
}

所以我认为你必须更改你的JSON数组,例如如下:

{
    [
        {
            "id": 0,
            "organizer": "Some Name",
            "eventStart": "09:30 AM",
            "eventEnd": "10:00 AM",
            "subject": "rental procedure",
            "attendees": "Some Name<br />Person 2<br />Person 3"
        },
        {
            "id": 1,
            "organizer": "Some Name",
            "eventStart": "09:30 AM",
            "eventEnd": "10:00 AM",
            "subject": "rental procedure",
            "attendees": "Some Name<br />Person 2<br />Person 3"
        },
        {
            "id": 2,
            "organizer": "Some Name",
            "eventStart": "09:30 AM",
            "eventEnd": "10:00 AM",
            "subject": "rental procedure",
            "attendees": "Some Name<br />Person 2<br />Person 3"
        }
    ]
}

您也可以决定不像我上面提到的那样使用字典列表,而是使用ID值作为每个字典的键;在这种情况下,你有:

{
    "id0":{       
            "organizer": "Some Name",
            "eventStart": "09:30 AM",
            "eventEnd": "10:00 AM",
            "subject": "rental procedure",
            "attendees": "Some Name<br />Person 2<br />Person 3"
    },
    "id1":{       
            "organizer": "Some Name",
            "eventStart": "09:30 AM",
            "eventEnd": "10:00 AM",
            "subject": "rental procedure",
            "attendees": "Some Name<br />Person 2<br />Person 3"
    },
    "id2":{       
            "organizer": "Some Name",
            "eventStart": "09:30 AM",
            "eventEnd": "10:00 AM",
            "subject": "rental procedure",
            "attendees": "Some Name<br />Person 2<br />Person 3"
    }
}