2011-12-12 124 views
1

我正在使用twitter api的某些部分,並使用ASIHTTPREQUEST獲取一些JSON響應。下面是JSON表示的樣子:iOS的複雜JSON響應

(
    { 
     contributors = "<null>"; 
     coordinates = "<null>"; 
     "created_at" = "Tue Nov 29 15:48:35 +0000 2011"; 
     entities =   { 
      hashtags =    (
      ); 
      media =    (
           { 
        "display_url" = "pic.twitter.com/650E1WRY"; 
        "expanded_url" = "http://twitter.com/ashu1702/status/141544088850796545/photo/1"; 
        id = 141544088854990848; 
        "id_str" = 141544088854990848; 
        indices =      (
         22, 
         42 
        ); 
        "media_url" = "http://p.twimg.com/AfbdmVBCEAAPJvT.jpg"; 
        "media_url_https" = "https://p.twimg.com/AfbdmVBCEAAPJvT.jpg"; 
        sizes =      { 
         large =       { 
          h = 279; 
          resize = fit; 
          w = 215; 
         }; 
         medium =       { 
          h = 279; 
          resize = fit; 
          w = 215; 
         }; 
         small =       { 
          h = 279; 
          resize = fit; 
          w = 215; 
         }; 
         thumb =       { 
          h = 150; 
          resize = crop; 
          w = 150; 
         }; 
        }; 
        type = photo; 
        url = "http://t.co/650E1WRY"; 
       } 
      ); 
      urls =    (
      ); 
      "user_mentions" =    (
      ); 
     }; 
     favorited = 0; 
     geo = "<null>"; 
     id = 141544088850796545; 
     "id_str" = 141544088850796545; 
     "in_reply_to_screen_name" = "<null>"; 
     "in_reply_to_status_id" = "<null>"; 
     "in_reply_to_status_id_str" = "<null>"; 
     "in_reply_to_user_id" = "<null>"; 
     "in_reply_to_user_id_str" = "<null>"; 
     place = "<null>"; 
     "possibly_sensitive" = 0; 
     "retweet_count" = 0; 
     retweeted = 0; 
     source = "<a href=\"http://www.apple.com\" rel=\"nofollow\">iOS</a>"; 
     text = "I am in Syria @(null) http://t.co/650E1WRY"; 
     truncated = 0; 
     user =   { 
      "contributors_enabled" = 0; 
      "created_at" = "Sun May 01 15:20:52 +0000 2011"; 
      "default_profile" = 1; 
      "default_profile_image" = 1; 
      description = "<null>"; 
      "favourites_count" = 0; 
      "follow_request_sent" = "<null>"; 
      "followers_count" = 0; 
      following = "<null>"; 
      "friends_count" = 5; 
      "geo_enabled" = 0; 
      id = 291164338; 
      "id_str" = 291164338; 
      "is_translator" = 0; 
      lang = en; 
      "listed_count" = 0; 
      location = "<null>"; 
      name = "Ashutosh Tiwari"; 
      notifications = "<null>"; 
      "profile_background_color" = C0DEED; 
      "profile_background_image_url" = "http://a0.twimg.com/images/themes/theme1/bg.png"; 
      "profile_background_image_url_https" = "https://si0.twimg.com/images/themes/theme1/bg.png"; 
      "profile_background_tile" = 0; 
      "profile_image_url" = "http://a2.twimg.com/sticky/default_profile_images/default_profile_3_normal.png"; 
      "profile_image_url_https" = "https://si0.twimg.com/sticky/default_profile_images/default_profile_3_normal.png"; 
      "profile_link_color" = 0084B4; 
      "profile_sidebar_border_color" = C0DEED; 
      "profile_sidebar_fill_color" = DDEEF6; 
      "profile_text_color" = 333333; 
      "profile_use_background_image" = 1; 
      protected = 0; 
      "screen_name" = ashu1702; 
      "show_all_inline_media" = 0; 
      "statuses_count" = 62; 
      "time_zone" = "<null>"; 
      url = "<null>"; 
      "utc_offset" = "<null>"; 
      verified = 0; 
     }; 
    } 
) 

我這裏是關心,其實之前設定的全部內容以數據模型,有沒有設置參數,以空字符串任何具體的方法是什麼?

就像有兩個實體一樣「time_zone」=「」;和「utc_offset」=「」;無論如何要迭代json響應並設置@「」空字符串。我不想在每個關鍵點都驗證NSNULL功能,並且它可能會導致我的應用程序在某個點出現一些循環漏洞,因此我只想弄清楚如何遍歷這些複雜的json來檢查每個實體併爲NSNull類型類設置空字符串。我曾嘗試過遞歸方式,但它只給出了上層關鍵字列表,而不是字典中的實體。

謝謝

+0

你確定這些行導致NSNull被反序列化?他們似乎是包含的字符串,這對你來說很好,不是嗎? –

+0

@HenriNormak我以同樣的方式思考,直到我開始通過我的數據模型將其分配給UITextFields。或者,也許當我在數據模型中分配我的NSDictionary時,它設置它?但沒有道理。當它看到這樣的參數 – kforkarim

回答

1

"<null>"值看起來是字符串,而不是NSNull。如果你想用""替換字符串,你可以使用stringByReplacingOccurrencesOfstring:withString:將它們轉換成大量的(作爲轉換你不想要的東西的一些風險)。

(但要肯定的,如果你提出了一些原始的JSON的,在VS的NSArray/NSDictionary的對象這顯然是你展示什麼樣的Objective-C的description這將是一件好事。)

+0

是正確的,我還假設他顯示NSObject.description的,因此我的答案和提示測試NSNull。 – Till

+0

@你是對的,我正在展示NSObject類型的描述。實際表示看起來像「Time_Zone」:null,這只是一個示例。所以基本上在nsobject的表示中顯示爲。當我使用uitextfield作爲timezone.text = json對象時,它給了我一個nsnull異常。我認爲這將是一個字符串類型,但我不明白爲什麼它會顯示這樣的異常。謝謝 – kforkarim

+0

引發異常是因爲您不能將[NSNull null]作爲NSString指向UI對象。 @「」!= [NSNull null]; – Till

1

不,你將不得不手動。根據[NSNull null]進行測試是最好的選擇。

+0

它的驚人之處時,它總是給出一個NSNull的例外,沒有任何功能可以實際將所有的null參數設置爲空字符串。只是更昂貴的工作。 – kforkarim

+0

@kforkarum請注意,您應該始終檢查外部數據是否過多。否則,你的應用程序將非常脆弱。對於JSON提要尤其如此。 – Till