JSON数据,编辑方式
Posted
技术标签:
【中文标题】JSON数据,编辑方式【英文标题】:JSON data, the way to edit it 【发布时间】:2014-10-06 14:57:05 【问题描述】:我需要获取 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"
我可以立即在选项中添加字符串吗?
【问题讨论】:
将 JSON 解析为 NSArrays/NSDictionarys,根据需要进行更新,然后序列化回 JSON。解析时一定要指定“可变容器”选项。 如前所述,你无法说出你想要什么。 【参考方案1】:如果你想编辑你的值,你应该使用 NSMutableDictionary
【讨论】:
你能给我举个例子 firstElm 和 secondElm 是一个 NSMutableDictionary 我不确定我是否理解您的问题。也许这可以帮助你***.com/questions/6931104/… 使用 NSJSONSerialization 解析时指定“可变容器”选项。【参考方案2】:试试这个:
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);
【讨论】:
这将附加一个以somekey
为键的对值,然后将修改后的数据添加到一个名为jsonModifiedOptions
的NSMutableArray
*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object”
@lhawks 它需要是一个可变数组,就像我的代码 NSMutableDictionary *data
你不能更改 NSArray
NSMutableArray *jsonModifiedOptions = [[NSMutableArray alloc] init]; for (NSMutableDictionary *data in [[[forTheDopOp objectForKey:@"options"] objectAtIndex:3] objectForKey:@"option_value"]) [data setObject:@"test" forKey:@"value"]; [jsonModifiedOptions addObject:data];
但是您访问的代码不同[[[forTheDopOp objectForKey:@"options"] objectAtIndex:3] objectForKey:@"option_value"]
@lhawks以上是关于JSON数据,编辑方式的主要内容,如果未能解决你的问题,请参考以下文章