如何从ns字典中的数组创建nsarray?

时间:2017-10-10 04:18:03

标签: objective-c json

我有一个json,有几个小时的操作。在那之下是一个小时列表。我希望列表成为我在索引处作为对象获得的数组。

"Hours_Of_Operation" = "{"daysofweek":"1", "open_time":"11:00:00", "close_time":"00:00:00"},{"daysofweek":"2", "open_time":"11:00:00", "close_time":"00:00:00"},{"daysofweek":"3", "open_time":"11:00:00", "close_time":"00:00:00"},{"daysofweek":"4", "open_time":"11:00:00", "close_time":"02:00:00"},{"daysofweek":"5", "open_time":"11:00:00", "close_time":"02:00:00"},{"daysofweek":"6", "open_time":"10:00:00", "close_time":"02:00:00"},{"daysofweek":"7", "open_time":"10:00:00", "close_time":"00:00:00"}";

1 个答案:

答案 0 :(得分:2)

//Your json should be like this
{"Hours_Of_Operation" : [{"daysofweek":"1", "open_time":"11:00:00", "close_time":"00:00:00"},{"daysofweek":"2", "open_time":"11:00:00", "close_time":"00:00:00"},{"daysofweek":"3", "open_time":"11:00:00", "close_time":"00:00:00"},{"daysofweek":"4", "open_time":"11:00:00", "close_time":"02:00:00"},{"daysofweek":"5", "open_time":"11:00:00", "close_time":"02:00:00"},{"daysofweek":"6", "open_time":"10:00:00", "close_time":"02:00:00"},{"daysofweek":"7", "open_time":"10:00:00", "close_time":"00:00:00"}]}

//Using this json you can parse data 
let arrayHoursOfOperation = yourJsonDictionary.objectForKey("Hours_Of_Operation") as! NSArray

您可以使用ComponentSeparatedByString来获得所需的结果。您甚至需要根据您的要求修改此代码。

NSString *str = @"{"daysofweek":"1", "open_time":"11:00:00", "close_time":"00:00:00"},{"daysofweek":"2", "open_time":"11:00:00", "close_time":"00:00:00"},{"daysofweek":"3", "open_time":"11:00:00", "close_time":"00:00:00"},{"daysofweek":"4", "open_time":"11:00:00", "close_time":"02:00:00"},{"daysofweek":"5", "open_time":"11:00:00", "close_time":"02:00:00"},{"daysofweek":"6", "open_time":"10:00:00", "close_time":"02:00:00"},{"daysofweek":"7", "open_time":"10:00:00", "close_time":"00:00:00"}";  
NSArray *arr = [str componentsSeparatedByString:@"{"];