2 bluetooth99 bluetooth99 于 2013.04.27 10:58 提问

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'?

谢谢

1个回答

KindAzrael
KindAzrael   2013.04.27 23:44
已采纳

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"];
suannai0314
suannai0314 帮你编辑了下代码格式,更多markdown的使用说明见http://ask.csdn.net/pages/markdown
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!