in my python server-side script i have the following
....
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
In my front-end using jquery/javascript i have this..
$.ajax({
type: 'REQUEST',
dataType: 'text',
url: 'json.py',
success: function(data){
console.log(data);
obj = JSON.parse(data);
},
}).done(function(){
console.log(obj)
})
});
my output is:
{"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"}
My issue
When i try to parse the json in javascript, i get an error code about
Unexpected token { in JSON at position
After doing some research, i found that i would have to send a json array as a response and not print a jsoned line for each event, otherwise my javascript wont be able to parse it and put it in an object.
My Goal
To get data from server and pass it to javascript as an object so that i can loop through that object and create an HTML element for each event.
I'm new to this JSON stuff, somehow i need to do the following, but i don't seem to know how to create a json array (that has the [ ] and the , at the end of each json/line.
[
{"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"}
]