gpt4 book ai didi

ios - 如何将JSON放入NSMutableArray

转载 作者:行者123 更新时间:2023-12-01 19:57:10 26 4
gpt4 key购买 nike

嗨,我是iOS开发的新手,目前在开发计划 Activity 时遇到问题

请让我知道如何添加JSON数据而不是硬编码数组。我只需要将json中的customer_name,customer_id替换为该硬编码数据

这是我的日程表按钮单击事件:

- (IBAction)schedule:(id)sender {

FFCalendarViewController *rootController = [FFCalendarViewController new];
[rootController setArrayWithEvents:[self arrayWithEvents]];


[self.navigationController pushViewController:rootController animated:YES];


}

这是我的硬编码数组:
- (NSMutableArray *)arrayWithEvents {

FFEvent *event1 = [FFEvent new];
[event1 setStringCustomerName: @"Customer A"];
[event1 setNumCustomerID:@1];
[event1 setDateDay:[NSDate dateWithYear:[NSDate componentsOfCurrentDate].year month:[NSDate componentsOfCurrentDate].month day:[NSDate componentsOfCurrentDate].day]];
[event1 setDateTimeBegin:[NSDate dateWithHour:10 min:00]];
[event1 setDateTimeEnd:[NSDate dateWithHour:15 min:13]];


FFEvent *event2 = [FFEvent new];
[event2 setStringCustomerName: @"Customer B"];
[event2 setNumCustomerID:@2];
[event2 setDateDay:[NSDate dateWithYear:[NSDate componentsOfCurrentDate].year month:[NSDate componentsOfCurrentDate].month day:[NSDate componentsOfCurrentDate].day]];
[event2 setDateTimeBegin:[NSDate dateWithHour:9 min:15]];
[event2 setDateTimeEnd:[NSDate dateWithHour:12 min:138]];

return [NSMutableArray arrayWithArray:@[event1, event2]];

}

这是我的json输出,我想替换为硬编码数组:
[  
{
"customer_id":"802",
"customer_name":"Tela"

},
{
"customer_id":"803",
"customer_name":"Bena"
}
]

这是我的序列化JSON代码:
NSData *myData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

NSMutableArray *testFeeds = [NSJSONSerialization JSONObjectWithData: myData options:NSJSONReadingMutableContainers error:nil];

NSMutableArray *rec = [[NSMutableArray alloc] init];

[rec addObjectsFromArray:testFeeds];

请帮助我解决此问题。

最佳答案

使用以下代码序列化json

NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];

现在 jsonArray包含 json文件的可变数据。因此,您可以在此数组中直接更改或更新值。

编辑
NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];

NSMutableArray *rec = [[NSMutableArray alloc] init];

for (int i = 0; i< jsonArray.count; i++) {
FFEvent *event1 = [FFEvent new];
[event1 setStringCustomerName:jsonArray[i][@"customer_name"]];
[event1 setNumCustomerID:[jsonArray[i][@"customer_id"] integerValue]];
[event1 setDateDay:[NSDate dateWithYear:[NSDate componentsOfCurrentDate].year month:[NSDate componentsOfCurrentDate].month day:[NSDate componentsOfCurrentDate].day]];
[event1 setDateTimeBegin:[NSDate dateWithHour:10 min:00]];
[event1 setDateTimeEnd:[NSDate dateWithHour:15 min:13]];

[rec addObject:event1];
}

关于ios - 如何将JSON放入NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41802097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com