程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-nsdictionary獲得指定key值

ios-nsdictionary獲得指定key值

編輯:編程綜合問答
nsdictionary獲得指定key值

在nsdictionary中保存json解析的結果:

{
 "statusCode":"200",
"body":[
  {
     "status":"success",
     "remarks":null
  }
],

"data":[
     "abcd":[
        {
           "category":"a",
           "title":"b",
           "id":"24"
        },
        {
           "category":"c",
           "title":"crd",
           "id":"65"
        },
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        }

     ]
  },
  {
     "efgh":[
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        },
        {
           "category":"sd",
           "title":"sd",
           "id":"1"
        }

                             ]
  },
  {
     "ijkl":[
        {
           "category":"ds",
           "title":"sd",
           "id":"18"
        },
        {
           "category":"sd",
           "title":"sd",
           "id":"1"
        }

     ]
  }
 ]
}

關鍵字@data的數據可以保存在數組中:

NSMutableArray *getdata=[[NSMutableArray alloc]init];
getcat=[results objectForKey:@"data"];

下面應該怎麼設置在第一個索引中的values(category, title, id)。比如'abcd'?

謝謝

最佳回答:


getdata 裡面應該是 3個NSDictionary 分別是

{
       "category":"a",
       "title":"b",
       "id":"24"
    },
    {
       "category":"c",
       "title":"crd",
       "id":"65"
    },
    {
       "category":"ds",
       "title":"sd",
       "id":"18"
    }

你要設置在第一個索引中的values

NSDictionary *firstDic = [getdata objectAtIndex:0];
 [firstDic setValue:@"new category" forKey:@"category"];
  [firstDic setValue:@"new title" forKey:@"title"];
  [firstDic setValue:@"new id" forKey:@"id"];
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved