Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 解析Json解析的代码是什么?_Iphone_Json_Ios5 - Fatal编程技术网

Iphone 解析Json解析的代码是什么?

Iphone 解析Json解析的代码是什么?,iphone,json,ios5,Iphone,Json,Ios5,您好,我已经实现了用于解析以下响应的代码,如下所示,但它工作不正常: NSString *req = [NSString stringWithFormat: @" My URL"]; NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue];

您好,我已经实现了用于解析以下响应的代码,如下所示,但它工作不正常:

 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"
    }
  ]
}

NSJSONSerialization类是仅适用于iOS 5及以上版本的本机类

对于任何iPhone OS版本,您都可以使用JSONKit:

NSJSONSerialization类是仅适用于iOS 5及以上版本的本机类

对于任何iPhone OS版本,您都可以使用JSONKit:

如果您的
resultsDict
包含上述JSON响应,则可以将其解析为:

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"]) ; 
}
NSString*req=[nsstringwithformat:@“我的URL”];
NSDictionary*googleResponse=[[NSString stringWithContentsOfURL:[NSURL URLWithString:req]编码:NSUTF8StringEncoding错误:NULL]JSONValue];
NSDictionary*resultsDict=[googleResponse valueForKey:@“eventtitle”];
NSMutableArray*resultArray=[resultsDict valueForKey:@“AlansHarleyEvents”];

对于(int i=0;i如果
resultsDict
包含上述JSON响应,则可以将其解析为:

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"]) ; 
}
NSString*req=[nsstringwithformat:@“我的URL”];
NSDictionary*googleResponse=[[NSString stringWithContentsOfURL:[NSURL URLWithString:req]编码:NSUTF8StringEncoding错误:NULL]JSONValue];
NSDictionary*resultsDict=[googleResponse valueForKey:@“eventtitle”];
NSMutableArray*resultArray=[resultsDict valueForKey:@“AlansHarleyEvents”];

对于(inti=0;iYu),可以将NSJSONSerialization对象与ios5一起使用

   NSDictionnary *jsonObject = [NSJSONSerialization JSONObjectWithData:resultsDict options:NSJSONReadingMutableContainers error:&error];

您可以将NSJSONSerialization对象与IOS 5一起使用

   NSDictionnary *jsonObject = [NSJSONSerialization JSONObjectWithData:resultsDict options:NSJSONReadingMutableContainers error:&error];

我认为否决票是因为
resultsDict
没有键/值
AlansHarleyEvents
@maulik:当我使用ur代码时,它在NSDictionary*googleResponse=[[NSString stringWithContentsOfURL:[NSURL URLWithString:req]编码:NSUTF8StringEncoding错误:null]点给了我空值JSONValue];我认为否决票是因为
resultsDict
没有键/值
AlansHarleyEvents
@maulik:当我使用ur代码时,它在NSDictionary*googleResponse=[[NSString StringWithContentsForl:[NSURL URLWithString:req]点给我空值编码:NSUTF8StringEncoding错误:NULL]JSONValue];