0

こんにちは、以下の応答を解析するためのコードを次のように実装しましたが、正しく機能していません。

 NSString *req = [NSString stringWithFormat: @" My URL"];   
 NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue];        
 NSDictionary *resultsDict = [googleResponse valueForKey:  @"eventtitle"];

以下の応答を解析するためのコードは何ですか? 解決策を教えてください。

{
  "AlansHarleyEvents": [
    {
      "id": "3",
      "eventtitle": "22nd Annual Pig Roast",
      "eventdate": "April 22nd 8am-5pm"
    },
    {
      "id": "4",
      "eventtitle": "Poker Run",
      "eventdate": "April 28th 8am at Shooters"
    },
    {
      "id": "5",
      "eventtitle": "Kickstands for kids",
      "eventdate": "May 12th 8am-5pm"
    },
    {
      "id": "6",
      "eventtitle": "Ride for the Cure",
      "eventdate": "May28th 8am Free Drinks!"
    },
    {
      "id": "7",
      "eventtitle": "Veterans Ride",
      "eventdate": "June 10th 9am @City Hall"
    },
    {
      "id": "8",
      "eventtitle": "Biker Beach Bash",
      "eventdate": "June 28th 8-5pm @ The Pier"
    },
    {
      "id": "10",
      "eventtitle": "22nd Annual Pig Roast",
      "eventdate": "April 22nd 8am-5pm"
    },
    {
      "id": "11",
      "eventtitle": "Poker Run",
      "eventdate": "April 28th 8am at Shooters Lounge"
    },
    {
      "id": "12",
      "eventtitle": "22nd Annual Pig Roast",
      "eventdate": "April 22nd 8am-5pm"
    },
    {
      "id": "13",
      "eventtitle": "Swamp Run",
      "eventdate": "April 22nd 8am-5pm"
    }
  ]
}
4

3 に答える 3

1

YuはIOS5でNSJSONSerializationオブジェクトを使用できます

   NSDictionnary *jsonObject = [NSJSONSerialization JSONObjectWithData:resultsDict options:NSJSONReadingMutableContainers error:&error];
于 2012-05-18T07:23:02.433 に答える
1

NSJSONSerialization クラスは、iOS 5 以降のみのネイティブ クラスです http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html

どの iPhone OS バージョンでも、JSONKit を使用できることを意味します: https://github.com/johnezang/JSONKit

于 2012-05-18T07:16:01.483 に答える
1

上記の JSON 応答が含まれている場合resultsDictは、次のように解析できます。

NSString *req = [NSString stringWithFormat: @" My URL"];
NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue];
NSDictionary *resultsDict = [googleResponse valueForKey: @"eventtitle"];

NSMutableArray *resultArray = [resultsDict valueForKey:@"AlansHarleyEvents"]; 

for(int i = 0; i<[resultArray count]; i++)
{
    NSLog(@"%@",[[resultArray objectAtIndex:i] valueForKey:@"id"]) ;
    NSLog(@"%@",[[resultArray objectAtIndex:i] valueForKey:@"eventtitle"]) ;
    NSLog(@"%@",[[resultArray objectAtIndex:i] valueForKey:@"eventdate"]) ; 
}
于 2012-05-18T07:16:03.087 に答える