我需要获取json数组并向其添加元素,然后将其发送回去。我的问题是如何编辑json数组元素?我能找到的唯一方法:
[firstElm addEntriesFromDictionary:[[forTheDopOp objectForKey:@"blablabla"] objectAtIndex:1]];
NSDictionary * dic = [NSDictionary dictionaryWithObject:DateAndTime forKey:@"value"];
[firstElm addEntriesFromDictionary:dic];
[order addObject:firstElm];
[secondElm addEntriesFromDictionary:[[forTheDopOp objectForKey:@"blablabla"] objectAtIndex:2]];
NSDictionary * dic1 = [NSDictionary dictionaryWithObject:backDateAndTime forKey:@"value"];
[secondElm addEntriesFromDictionary:dic1];
[orderOpSender addObject:secondElm];
但如果我需要在循环中执行它,它就不起作用
for (int i = 0; i < [[[[forTheDopOp objectForKey:@"blablabla"] objectAtIndex:3] objectForKey:@"option_value"] count]; i++) {
[valuesElm addEntriesFromDictionary:[[[[forTheDopOp objectForKey:@"blablabla"] objectAtIndex:3] objectForKey:@"blablabla"] objectAtIndex:i]];
NSDictionary * dic = [NSDictionary dictionaryWithObject:[[[[[forTheDopOp objectForKey:@"blablabla"] objectAtIndex:3] objectForKey:@"blablabla"] objectAtIndex:i] objectForKey:@"name"] forKey:@"value"];
[valuesElm addEntriesFromDictionary:dic];
[orderOp addObject:valuesElm];
}
最后,我得到了一组相同的元素。
我的json的例子:
{ "product_id": 83, "firstname":"111", "lastname": "222", "email": "e2e2e@wsdwsd.ru", "telephone":"089234","options":[
{
"name":"\u0414\u0430\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f",
"type":"datetime",
"option_value":"",
"required":"1",
"value": "2014-09-01 9:10",
"product_option_id":"263",
"option_id":"15"
},
{
"name":"\u0414\u0430\u0442\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430",
"type":"datetime",
"option_value":"",
"required":"1",
"product_option_id":"264",
"value": "2014-10-01 10:10",
"option_id":"16"
}
我可以立即在选项中添加字符串吗?
答案 0 :(得分:0)
如果要编辑值,则应使用NSMutableDictionary
答案 1 :(得分:0)
试试这个:
NSMutableArray *newForTheDopOp = [[NSMutableArray alloc]init];
for(NSDictionary *option in forTheDopOp[@"options"])){
NSLog(@"option = %@", option);
NSMutableDictionary *newOption = [[NSMutableDictionary alloc] init];
[newOption addEntriesFromDictionary:option];
[newOption setObject:@"test" forKey:@"option_value"];
[newForTheDopOp addObject:newOption];
NSLog(@"newOption = %@", newOption);
}
NSLog(@"newForTheDopOp = %@", newForTheDopOp);