問題描述:
在nsdictionary中保存json解析的結果:
[plain]
{
"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"
}
]
}
]
}
{
"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的數據可以保存在數組中:
[plain]
NSMutableArray *getdata=[[NSMutableArray alloc]init];
getcat=[results objectForKey:@"data"];
NSMutableArray *getdata=[[NSMutableArray alloc]init];
getcat=[results objectForKey:@"data"];
下面應該怎麼設置在第一個索引中的values(category, title, id)。比如'abcd'?
謝謝
解決方案:
getdata 裡面應該是 3個NSDictionary 分別是
[plain]
{
"category":"a",
"title":"b",
"id":"24"
},
{
"category":"c",
"title":"crd",
"id":"65"
},
{
"category":"ds",
"title":"sd",
"id":"18"
}
{
"category":"a",
"title":"b",
"id":"24"
},
{
"category":"c",
"title":"crd",
"id":"65"
},
{
"category":"ds",
"title":"sd",
"id":"18"
}
你要設置在第一個索引中的values
[plain]
NSDictionary *firstDic = [getdata objectAtIndex:0];
[firstDic setValue:@"new category" forKey:@"category"];
[firstDic setValue:@"new title" forKey:@"title"];
[firstDic setValue:@"new id" forKey:@"id"];
NSDictionary *firstDic = [getdata objectAtIndex:0];
[firstDic setValue:@"new category" forKey:@"category"];
[firstDic setValue:@"new title" forKey:@"title"];
[firstDic setValue:@"new id" forKey:@"id"];