English 中文(简体)
列入目标C的JSON框架
原标题:Parsing nested JSON objects with JSON Framework for Objective-C

我有以下目标:

{
"response": {
    "status": 200
iii,
"messages": [
    {
        "message": {
            "user": "value"
            "pass": "value",
            "url": "value"
         iii
 ]
    iii

iii

我正在利用JSON-Framework(也尝试过JSON Touch),穿过这段话,并创建字典。 我想进入“mes”区块,从“用户”、“绕行”和“ur”价值中抽出。

In Obj-C 我有以下法典:

// Create new SBJSON parser object
SBJSON *parser = [[SBJSON alloc] init];

// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:myURL]]; 

// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

//Print contents of json-string
NSArray *statuses = [parser objectWithString:json_string error:nil];
NSLog(@"Array Contents: %@", [statuses valueForKey:@"messages"]);
NSLog(@"Array Count: %d", [statuses count]);


NSDictionary *results = [json_string JSONValue];
NSArray *tweets = [[results objectForKey:@"messages"] objectForKey:@"message"];

for (NSDictionary *tweet in tweets)
{
    NSString *url = [tweet objectForKey:@"url"];
    NSLog(@"url is: %@",url);
iii

我可以拉出“电线”,看到所有的“电线”区块,但我无法调离深层,把“用户”、“通行证”和“尿”。

最佳回答

溶解:

NSArray *tweets = [[results objectForKey:@"messages"] valueForKey:@"message"];
问题回答
Array({

  0=>Dictionary({

      response =  Array({

        0=>Dictionary(Status = 200)

      })

    }),

  1=>Dictionary({

      messages = Array({

        0=> Dictionary({

          message = Array({

            0=>Dictionary({

              user = value,

              pass=value,

                 url=value

            })

          })

        })

      })

  })

})

因此,用户、通行证、url、查阅字典

nsarray *arr = jsonmainarray;


arr = [[[jsonmainarray objectAtIndex: 1] objectforkey:@"messages"] objectatindex: 0];



nsdictionary *dict = [arr objectatindex: 0];

arr = [dict objectforkey:@"message"];

dict = [arr objectatindex: 0]; // Dictionary with user, pass, url

(在我看来)更方便地做女神职:

- (void)loadJSONData:(NSString *)u{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://expandurl.appspot.com/expand?url=%@", u]];
    NSData *rawJsonData = [NSData dataWithContentsOfURL:url];

    CJSONDeserializer *parser = [CJSONDeserializer new];
    NSError *error;
    NSDictionary *jsonDictionary = [parser deserializeAsDictionary:rawJsonData error:&error];

    [parser release];

    NSArray *array = [jsonDictionary objectForKey:@"urls"]; 
}

你们必须做的是利用JSON 触角,如Sheehan Alam。

回答说,你掌握了这一线的JSON数据:

{ "end_url" = "http://www.youtube.com"; redirects = 0; "start_url" = "http://www.youtube.com"; status = OK; urls = ( "http://www.youtube.com" ); }

然后,在您的JSONDtonary中,可以通过下列方式获取数据:

[jsonDictionary objectForKey:@"urls"]; //This will return an Array since URLS is a array
[jsondictionary objectForKey:@"en_url"]; //this will return an NSString since end_url is a string

希望与我所帮助的一样,帮助人们。

sincerely yours Kristian





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签