nsdictionary獲得指定key值

原問題來自於CSDN問答頻道,更多解決方案見:http://ask.csdn.net/questions/2265

問題描述:

在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"];




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章