I am trying to parse this from a webserver to a tableview in iOS
{
"transactions": [
{
"ID": "350",
"description": "Macbook Pro 17 inch",
"price": "2811.83"
},
{
"ID": "351",
"description": "Macbook - white",
"price": "1720.10"
},
{
"ID": "352",
"description": "iPad 2",
"price": "650.00"
},
{
"ID": "353",
"description": "Macbook Pro 17 inch",
"price": "3233.98"
},
{
"ID": "354",
"description": "Macbook Pro 15 inch",
"price": "2100.55"
},
{
"ID": "355",
"description": "Macbook Air",
"price": "899.99"
},
{
"ID": "356",
"description": "Mac Pro",
"price": "3400.77"
}
]
}
The only things I want from this are the descriptions and the prices. I need to add up the price of each item and get a total. Each transaction name needs to be stored in an NSArray and then displayed in a UITableView.
Any help? The JSONkit doesnt give me the SBJSON parser
I really liked JSONKit because of its simplicity and speed. You have to realize, what it returns is the top level construct as an object – so you have to think about it. It will be a NSDictionary (so you can typecast it), and it will have one key “transactions”.
This key will return an NSArray of NSDictionary objects, each of which will have the keys ID, description, and price.
So something like this (where
itemListDatais the JSON data fetched from the URL for example):So, we took the raw JSON data, instantiated a decoder, then decoded it into a dictionary – and retrieved the 1 object in the dictionary (as an array in this case, because that’s what it is). Not too bad for a newbie, eh?
(I should add – the reason I make an immutable copy of the NSData, is that this code snippet is in an asynchronous download, triggered in the
connectionDidFinishLoadingmethod.)