2013-10-11 52 views
-3

,我需要使用Objective-C的解析JSON使用Objective-C的

幫助解析JSON這是我的JSON:

{ 
    "days": [ 
     { 
      "reference": "13L6-A67-1", 
      "period": "", 
      "dayinit": "4", 
      "hourinit": "9", 
      "minuteinit": "30", 
      "dayend": "4", 
      "hourend": "10", 
      "minuteend": "30", 
      "totalattendance": "5", 
      "currentattendance": "5", 
      "teacher_surname": "jones", 
      "teacher_forenames": "Carol", 
      "room": "C109" 
     }, 
     { 
      "reference": "13NAPUSD-A1", 
      "period": "", 
      "dayinit": "3", 
      "hourinit": "10", 
      "minuteinit": "45", 
      "dayend": "3", 
      "hourend": "11", 
      "minuteend": "45", 
      "totalattendance": "3", 
      "currentattendance": "3", 
      "teacher_surname": "Carol", 
      "teacher_forenames": "Nicola", 
      "room": "M139" 
     }, 
     { 
      "reference": "13NASUWO-X1", 
      "period": "", 
      "dayinit": "3", 
      "hourinit": "14", 
      "minuteinit": "40", 
      "dayend": "3", 
      "hourend": "15", 
      "minuteend": "5", 
      "totalattendance": "0", 
      "currentattendance": "0", 
      "teacher_surname": "", 
      "teacher_forenames": "", 
      "room": "" 
     }, 
     { 
      "reference": "13NASUWO-X1", 
      "period": "", 
      "dayinit": "5", 
      "hourinit": "13", 
      "minuteinit": "35", 
      "dayend": "5", 
      "hourend": "14", 
      "minuteend": "0", 
      "totalattendance": "0", 
      "currentattendance": "0", 
      "teacher_surname": "", 
      "teacher_forenames": "", 
      "room": "" 
     }]} 

然後我需要將其過濾成不同的 「Dayinit」 鍵獨立數組。 我只是不知道從哪一個開始。

到目前爲止,我只在YouTube上發現了關鍵的視頻上解析JSON但他們沒有說明如何過濾數據已經解析

+1

如果您已經可以解析JSON,那麼您應該顯示您嘗試的內容以及得到的距離。 –

+1

這實際上與'xcode IDE'有什麼關係? – Popeye

+0

查看mycode .. – Jitendra

回答

9

你需要拿到鑰匙,按您的JSON。使用該密鑰的 您將獲得數據。

NSURL * url=[NSURL URLWithString:@"http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo"]; // pass your URL Here. 

     NSData * data=[NSData dataWithContentsOfURL:url]; 

     NSError * error; 

     NSMutableDictionary * json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error]; 

     NSLog(@"%@",json); 


     NSMutableArray * referanceArray=[[NSMutableArray alloc]init]; 

     NSMutableArray * periodArray=[[NSMutableArray alloc]init]; 

     NSArray * responseArr = json[@"days"]; 

     for(NSDictionary * dict in responseArr) 
     { 

      [referanceArray addObject:[dict valueForKey:@"reference"]]; 
      [periodArray addObject:[dict valueForKey:@"period"]]; 

     } 


     NSLog(@"%@",referanceArray); // Here you get the Referance data 
     NSLog(@"%@",periodArray);  // Here you get the Period data 

在這裏,您使用

[referanceArray addObject:[dict valueForKey:@"reference"]]; 

使用所有這些鍵獲取數據。

試試看看這個代碼。

+1

以下是一個有用的資源:http://www.raywenderlich.com/5492/working-with-json-in-ios-5 – Carl

0

後,您可以通過JSON的使用`迭代得到它值。

對於剛剛參考:

ContentDict = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil]; 

days = [ContentDict objectForKey:@"days"]; 
    // 3. iterate the array; each element is a dictionary... 
        for (NSDictionary *lesson in days) 
        { 

         NSString *content = [lesson objectForKey:@"reference"]; 
         NSString *content2 = [lesson objectForKey:@"dayinit"]; 
         // ........ 
          ........ 
          ....... 

         NSLog(@"Reference : %@", content); 

        } 
0

您可以分析您的JSON像

NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil]; 
    NSMutableArray *jsonParseArray = [[NSMutableArray alloc] initWithArray:[jsonDic objectForKey:@"days"]]; 

然後用objectAtIndex:獲得陣列中的每個元素放入NSDictionary的。

NSDictionary *selectedDic = [jsonParseArray objectAtIndex:0]; 

你可以得到解釋的價值簡單地objectForKey你的鑰匙是

 "reference": 
     "period": 
     "dayinit": 
     "hourinit": 
     "minuteinit": 
     "dayend": 
     "hourend": 
     "minuteend": 
     "totalattendance": 
     "currentattendance": 
     "teacher_surname": 
     "teacher_forenames": 
     "room":