开发者

ios array to dictionary or to array

Im getting a JSON response to my app [Twitter web service], is a string, but for example the object at index 0 is:

es array en:0, tiene {
contributors = "<null>";
coordinates = "<null>";
"created_at" = "Thu Aug 04 23:26:05 +0000 2011";
favorited = 0;
geo = "<null>";
id = 99259843982016513;
"id_str" = 99259843982016513;
"in_reply_to_screen_name" = "<nul开发者_运维技巧l>";
"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://twitter.com/tweetbutton\" rel=\"nofollow\">Tweet Button</a>";
text = "Stack Exchange Q&A site proposal: Freelance Workers http://t.co/yaW1RHp";
truncated = 0;
user =     {
    "contributors_enabled" = 0;
    "created_at" = "Mon Jul 13 19:39:31 +0000 2009";
    "default_profile" = 0;
    "default_profile_image" = 0;
    description = "My goal is to enable the brain computer interfaces to use the possibilities of mobile platforms for robotics and physical computing";
    "favourites_count" = 0;
    "follow_request_sent" = "<null>";
    "followers_count" = 92; ...

so there are like 17 objects for my array [for each twitt], so how can I decompose those objects into further arrays or a dictionary?

I specifaclly want the text key

text = "Apple vs Samsung tablets [haha and Samsung is an Apple hardware provider!!]\nhttp://t.co/rvv43Hy";

thanks a lot


There may already be a parser built for this, but if not I think you'll find the following method useful.

NSArray *strings = [input componentsSeparatedByString:@";"];

It returns an array of strings with (in this case) ";" as a delimiter.

{contributors = "" , coordinates = "", ...}

You can them separate them further:

NSDictionary *dict = [NSDictionary dictionary];
for (NSString *s in strings)
{
    NSArray *keyValue = [s componentsSeparatedByString:@"="];
    NSString *key = [keyValue objectAtIndex:0];
    NSString *value = [keyValue objectAtIndex:1];
    [dict setValue:value forKey:key];
}

There does seem to be some extra data at the start of the response, you might have to strip this off first.


You would, eg, extract the "user" object from your outer dictionary and assign that to an NSDictionary variable. Then you can extract, say, "default_profile" from the second dictionary.

One could probably write a simple "path navigator" tool that would access an individual entity via "path notation" without having to explicitly extract the component pieces, but I don't know of a "canned" one.


just in case someone else is looking for this, I ended up using the

open-source JSON Framework by Stig Brautaset. with its parser, and followed some instructions from here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜