2011-12-06 51 views
1

我有以下數據,我想打印具有以下結構的項目列表。如何使用以下格式解析JSONKit中的數據

讓我知道我該怎麼做。

我無法使用以下語法獲取數據。

/* Data to Row Json from URL*/ 
NSString *MyRowJson = [NSString stringWithContentsOfURL:url 
               encoding:NSUTF8StringEncoding error:nil]; 

/* Copy data to Items from MyRowJson*/ 
NSDictionary *items = [MyRowJson objectFromJSONStringWithParseOptions:true]; 

/*get the deals data*/ 
NSMutableArray *ResponseData = [items objectForKey:@"deals"]; 

/*Get the count and based on this loop through the objects.*/  
NSLog(@"deals data count is %d",[ResponseData count]); 

這裏我在打印計數時得到了異常。

以下是我的數據結構。

{ 
    "meta": { 
     "code": 200 
    }, 
    "response": { 
     "deals": [ 
      { 
       "id": 32373, 
       "date_added": "2011-01-13 12:12:50", 
       "end_date": "2011-01-14 10:00:00", 
       "active": 1, 
       "discount": { 
        "raw": 71, 
        "formatted": "71%" 
       }, 
       "price": { 
        "raw": "85.00", 
        "formatted": "$85" 
       }, 
       "value": { 
        "raw": "300.00", 
        "formatted": "$300" 
       }, 
       "title": "$85 For $300 Babyface Facial At Park Avenue MedSpa", 
       "yahoo_title": "71% off Babyface Facial", 
       "url": "http://yahoo.com/aff/click/?deal=AvwTADtE&key=F374EFbM", 
       "yahoo_url": "http://yahoo.com/new-york/livingsocial/85-for-300-babyface-facial-at-park-avenue-medspa/", 
       "mobile_url": "http://m.yahoo.com/new-york/livingsocial/85-for-300-babyface-facial-at-park-avenue-medspa/", 
       "images": { 
        "image_big": "http://d22nv2k05ynu7x.cloudfront.net/deal_images/deal/85-for-300-babyface-facial-at-park-avenue-medspa-1294920769_display_image.jpg", 
        "image_small": "http://d22nv2k05ynu7x.cloudfront.net/deal_images/deal/85-for-300-babyface-facial-at-park-avenue-medspa-1294920769_small_image.jpg" 
       }, 
       "division": { 
        "slug": "new-york", 
        "name": "New York", 
        "active": 1, 
        "time_zone_diff": -4, 
        "lat": "40.7142690000000000", 
        "lon": "-74.0059730000000000", 
        "url": "http://yahoo.com/new-york/" 
       }, 
       "tags": [ 
        { 
         "name": "Facial", 
         "slug": "facial", 
         "url": "http://yahoo.com/new-york/deals/facial/" 
        }, 
        { 
         "name": "Spa", 
         "slug": "spa", 
         "url": "http://yahoo.com/new-york/deals/spa/" 
        } 
       ], 
       "business": { 
        "name": "Park Avenue MedSpa", 
        "url": "", 
        "locations": [ 
         { 
          "address": "565 Park Ave", 
          "locality": "New York", 
          "phone": "212-593-8821", 
          "lat": null, 
          "lon": null, 
          "state": NY, 
          "zip_code": "11211" 
         } 
        ] 
       }, 
       "source": { 
        "name": "LivingSocial", 
        "slug": "livingsocial", 
        "paid": 0, 
        "url": "http://yahoo.com/new-york/livingsocial" 
       } 
      } 
     ] 
    } 
} 
+1

您發佈的JSON無效,請檢查您的自我http://jsonlint.com。問題是「狀態」:NY'需要引用NY''「狀態」:「NY」' – rckoenes

+1

Cosider它已更新,讓我知道如何前進,因爲這是從實際數據中摘錄的,數據是巨大,所以我無法在這裏粘貼,這可能是一種類型...請讓我知道。 thanks –

回答

3

你正確pasring數據和你搶,從最頂端節點的交易,但它是響應節點下:

NSString *jsonData = [NSString stringWithContentsOfURL:url 
               encoding:NSUTF8StringEncoding error:nil]; 
NSError *error = nil; 
NSDictionary *items = [jsonData JSONObjectWithData:data options:0 error:&error]; 

if (!items) { 
    NSLog(@"Could not parse json:%@", error); 
    return; 
} 
/*get the deals data*/ 
NSDictionary *response = [items objectForKey:@"response"]; 
NSArray *dealsArray = [response objectForKey:@"deals"]; 

/*Get the count and based on this loop through the objects.*/  
NSLog(@"deals data count is %d",[dealsArray count]); 

對象還返回來回JSON是inmutable,因此你不能將它們分配給可變對象。 您將需要使用copyMutable無法修改類型的方法來獲取可變類型。 請記住copyMutable需要在非ARC項目中發佈。

+1

感謝您的超級快速回答:)雖然打破了我遇到的山磚後我如何列出每個部分的內部屬性與交易細分市場。請幫忙。如何循環思考數據,並把他們的細分信息 –

+0

我想打印商業名稱..我鑽了下來,以獲得具有四個元素的業務數組,現在想要的名稱。 NSArray * BusinessArray = [[dealsArray objectAtIndex:i] objectForKey:@「business」];一些代碼打印名稱 –