如何解析此响应并保存到iphone中的数组中

时间:2012-01-30 12:17:30

标签: iphone objective-c ipad

我是objective-c的新手,我已经完成了NSXML解析,但是如何解析这个响应。答复是:

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] => full
                                )

                        )

                )

            [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] => full
                                )

                        )

                )
        )

2 个答案:

答案 0 :(得分:3)

您发布的响应看起来像是数组的print_r

正如Terente在他的评论中所说,解决这个问题的最简单方法是让服务器用JSON编码这个数组。

这非常简单,所有客户端都需要做服务器端更换

print_r($array);

json_encode($array);

然后,您将能够使用iOS 5 JSON框架或任何外部JSON框架(YAJLiOS,JSONKit,SBJSON等...)来轻松解析响应。

修改

您在问题评论http://staging.paddle8.com/api_v1/artworks/get_gallery_artworks?gallery_id=19中发布的链接确实会返回JSON。

要解析此问题,您需要使用JSON框架。如果你的应用程序需要与5.0以下的iOS版本兼容,我建议你使用JSONKit框架,它已被证明是最快的JSON解析器。

您可以在此处获取:https://github.com/johnezang/JSONKit

将此框架导入项目后,您可以像这样解析JSON响应:

NSString *jsonString = yourResponseString; // yourResponseString is the NSString object you get in response to your call to the API

NSDictionary *dict = [jsonString objectFromJSONString]; // This will return either an NSDictionary or NSArray depending on the structure of the jsonString, in your case, this will be a NSDictionary

// Now to get the array of "properties"
NSArray *propertiesArray = [dict objectForKey:@"properties"];

// Now you have an NSArray will all the "properties" objects in the JSON, you can cycle through this array to create all your objects accordingly

for(int i=0; i<[propertiesArray count]; i++) {

    // get the dictionary for each properties object
    NSDictionary *propertyDict = [propertiesArray objectAtIndex:i];

    // now you can access all the variables in the properties object

    // id
    int id = [[propertyDict objectForKey:@"id"] intValue];

    // title
    NSString *title = [propertyDict objectForKey:@"title"];

    // etc ...

}

希望这有帮助

答案 1 :(得分:1)

正如@Mutix和@Terente一样,Ionut Alexandru指出,它是一个JSON输出。试试这个链接,它展示了如何在IOS中进行JSON解析。

http://www.touch-code-magazine.com/tutorial-fetch-and-parse-json/