亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何用Object-C解析JSON?

如何用Object-C解析JSON?

汪汪一只貓 2019-06-29 18:08:50
如何用Object-C解析JSON?我對iPhone很陌生。有人能告訴我解析這些數據并獲取活動細節、名字和姓氏所遵循的步驟嗎?{     "#error": false,      "#data": {         "": {             "activity_id": "35336",              "user_id": "1",              "user_first_name": "Chandra Bhusan",              "user_last_name": "Pandey",              "time": "1300870420",              "activity_details": "Good\n",              "activity_type": "status_update",              "photo_url": "http://184.73.155.44/hcl-meme/QA_TEST/sites/default/files/pictures/picture-1627435117.jpg"         },          "boolean": "1",          "1": {             "1": {                 "photo_1_id": "9755"             },              "activity_id": "35294",              "album_name": "Kalai_new_Gallery",              "user_id": "31",              "album_id": "9754",              "user_first_name": "Kalaiyarasan",              "user_last_name": "Balu",              "0": {                 "photo_0_id": "9756"             },              "time": "1300365758",              "activity_type": "photo_upload",              "photo_url": "http://184.73.155.44/hcl-meme/QA_TEST/"         },          "3": {             "activity_id": "35289",              "user_id": "33",              "user_first_name": "Girija",              "user_last_name": "S",              "time": "1300279636",              "activity_details": "girija Again\n",              "activity_type": "status_update",              "photo_url": "http://184.73.155.44/hcl-meme/QA_TEST/sites/default/files/pictures/picture-33-6361851323080768.jpg"         },
查看完整描述

3 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

從OSXv10.7和iOS 5的角度來看,現在推薦的第一件事可能是NSJSONSerialization,蘋果提供了JSON解析器。如果在運行時發現類不可用,則只使用第三方選項作為后盾。

例如:

NSData *returnedData = ...JSON data, probably from a web request...// probably check here that returnedData isn't nil; attempting// NSJSONSerialization with nil data raises an exception, and who// knows how your third-party library intends to react?if(NSClassFromString(@"NSJSONSerialization")){
    NSError *error = nil;
    id object = [NSJSONSerialization
                      JSONObjectWithData:returnedData
                      options:0
                      error:&error];

    if(error) { /* JSON was malformed, act appropriately here */ }

    // the originating poster wants to deal with dictionaries;
    // assuming you do too then something like this is the first
    // validation step:
    if([object isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *results = object;
        /* proceed with results as you like; the assignment to
        an explicit NSDictionary * is artificial step to get 
        compile-time checking from here on down (and better autocompletion
        when editing). You could have just made object an NSDictionary *
        in the first place but stylistically you might prefer to keep
        the question of type open until it's confirmed */
    }
    else
    {
        /* there's no guarantee that the outermost object in a JSON
        packet will be a dictionary; if we get here then it wasn't,
        so 'object' shouldn't be treated as an NSDictionary; probably
        you need to report a suitable error condition */
    }}else{
    // the user is using iOS 4; we'll need to use a third-party solution.
    // If you don't intend to support iOS 4 then get rid of this entire
    // conditional and just jump straight to
    // NSError *error = nil;
    // [NSJSONSerialization JSONObjectWithData:...}


查看完整回答
反對 回復 2019-06-29
?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

NSString* path  = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"json"];//將文件內容讀取到字符串中,注意編碼NSUTF8StringEncoding 防止亂碼,NSString* jsonString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];//將字符串寫到緩沖區。NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];NSError *jsonError;id allKeys = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONWritingPrettyPrinted error:&jsonError];for (int i=0; i<[allKeys count]; i++) {
    NSDictionary *arrayResult = [allKeys objectAtIndex:i];
    NSLog(@"name=%@",[arrayResult objectForKey:@"storyboardName"]);}

檔案:

 [
  {
  "ID":1,
  "idSort" : 0,
  "deleted":0,
  "storyboardName" : "MLMember",
  "dispalyTitle" : "76.360779",
  "rightLevel" : "10.010490",
  "showTabBar" : 1,
  "openWeb" : 0,
  "webUrl":""
  },
  {
  "ID":1,
  "idSort" : 0,
  "deleted":0,
  "storyboardName" : "0.00",
  "dispalyTitle" : "76.360779",
  "rightLevel" : "10.010490",
  "showTabBar" : 1,
  "openWeb" : 0,
  "webUrl":""
  }
  ]


查看完整回答
反對 回復 2019-06-29
  • 3 回答
  • 0 關注
  • 1477 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號