2011-12-12 73 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