How to browse a specific JSON object
thierrybx opened this issue · comments
thierrybx commented
Hello,
I can create an object from a string of the following style:
let msg="[{"size":"37","id":"127"},{"size":"38","id":"129"}]"
I want to read each object, object by object from a JSON array object to do some stuff
var obj:[AnyObject] = []
var json = JSON(obj)
if let dataFromString = msg.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
NSLog("%@ - %@", "POST_MESSAGE", " ** List of ID **")
json = JSON(data: dataFromString)
//// The following two lines work
NSLog("%@ - %@", "POST_json", json[0]["id"].toString())
NSLog("%@ - %@", "POST_json", json[1]["id"].toString())
////////////// BUT when i try to get objects one by one, it is not working...
for o in json{
///// Not Working HERE
NSLog("%@ - %@", "POST_json EACH", o["id"].toString())
}
}
do you know how I can get to know the number of objects in my JSON array? (Here 2)
And also, how can I browse Object by Object? (json[n])
Thank you in advance, I tried many combinations without success ...
PS: Sorry for my poor english
thierrybx commented
Edit: I found for the number of objects: json.length
but i still not found for the loop... maybe using asArray?