objective c - How to parse this response and save into array in iphone -
objective c - How to parse this response and save into array in iphone -
i new in objective-c, have done nsxml parsing, how parse response. response is:
array ( [success] => 1 [artworks] => array ( [0] => array ( [id] => 105 [title] => asdasdfg [height] => 0.000 [width] => 0.000 [depth] => 0.000 [medium] => [list_price] => 0 [status] => draft [edition] => [editions] => [artist_proofs] => [displaydate] => [created] => 2011-05-23 16:36:56 [hash] => 98a0b94ad30cdda90f9a8195722869db [artist] => array ( [first_name] => kcho [last_name] => ) [category] => [images] => array ( [primary] => array ( [location] => http://staging.paddle8.com/assets/img/placeholder/145x145.jpg [width] => 145 [height] => 145 [type] => total ) ) ) [1] => array ( [id] => 104 [title] => asdasdfg [height] => 23.000 [width] => 223.000 [depth] => 0.000 [medium] => oil on canvas [list_price] => 1 [status] => draft [edition] => [editions] => [artist_proofs] => [displaydate] => 2009 [created] => 2011-05-23 12:36:10 [hash] => 98a0b94ad30cdda90f9a8195722869db [artist] => array ( [first_name] => kcho [last_name] => ) [category] => [images] => array ( [primary] => array ( [location] => http://staging.paddle8.com/assets/img/placeholder/145x145.jpg [width] => 145 [height] => 145 [type] => total ) ) ) )
the response posted looks php print_r
of array.
as terente suggested in comment, easiest way parse have server encode array in json.
this trivial, client need server side replace
print_r($array);
with
json_encode($array);
you able utilize ios 5 json framework or external json frameworks (yajlios, jsonkit, sbjson, etc ...) parse response.
edit:
the link posted in question comments http://staging.paddle8.com/api_v1/artworks/get_gallery_artworks?gallery_id=19 indeed homecoming json.
to parse this, need utilize json framework. if app needs compatible ios versions below 5.0, suggest utilize jsonkit framework, has been shown fastest json parser out there.
you can here: https://github.com/johnezang/jsonkit
once have imported framework project, can parse json response this:
nsstring *jsonstring = yourresponsestring; // yourresponsestring nsstring object in response phone call api nsdictionary *dict = [jsonstring objectfromjsonstring]; // homecoming either nsdictionary or nsarray depending on construction of jsonstring, in case, nsdictionary // array of "properties" nsarray *propertiesarray = [dict objectforkey:@"properties"]; // have nsarray "properties" objects in json, can cycle through array create objects accordingly for(int i=0; i<[propertiesarray count]; i++) { // dictionary each properties object nsdictionary *propertydict = [propertiesarray objectatindex:i]; // can access variables in properties object // id int id = [[propertydict objectforkey:@"id"] intvalue]; // title nsstring *title = [propertydict objectforkey:@"title"]; // etc ... }
hope helps
iphone objective-c ipad
Comments
Post a Comment