如何在JSON中的单个主对象中创建多个对象?

时间:2015-05-22 08:12:52

标签: ios arrays json xcode object

我有3个数组,我想在多个对象中转换:

NSArray *finalJSONArray = @[@"0",@"3",@"6",@"8"];
NSArray *time = @[@"12am",@"1am",@"2am",@"3am"];
NSArray *time = @[@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday"];

现在我想用jSON字符串转换这两个数组,如下所示:

{
"Sunday": {
    "0": "12 AM",
    "3": "1 AM",
    "6": "2 AM",
    "8": "3 AM"
},
"Monday": {
    "0": "12 AM",
    "3": "1 AM",
    "6": "2 AM",
    "8": "3 AM"
},
"Tuesday": {
    "0": "12 AM",
    "3": "1 AM",
    "6": "2 AM",
    "8": "3 AM"
},
"Wednesday": {
    "0": "12 AM",
    "3": "1 AM",
    "6": "2 AM",
    "8": "3 AM"
},
"Thursday": {
    "0": "12 AM",
    "3": "1 AM",
    "6": "2 AM",
    "8": "3 AM"
}
}

这里是我的代码现在没有填充,我尝试了不同的方法来填充这些NSDictionaries但不能得到相同的结果:

// Empty array
NSDictionary *emptyArray = @{};

// Single element array
NSDictionary *singleElementArray = @{};

// Array of above arrays
NSArray *arrayOfObjects = @[emptyArray, singleElementArray];

// Dictionary with several kay/value pairs and the above array of arrays
NSDictionary *dict = @{@"Sunday" : arrayOfObjects};

NSError *error = nil;
NSData *json;

// Dictionary convertable to JSON ?
if ([NSJSONSerialization isValidJSONObject:dict])
{
    // Serialize the dictionary
    json = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

    // If no errors, let's view the JSON
    if (json != nil && error == nil)
    {
        NSString *jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding];

        NSLog(@"JSON: %@", jsonString);
    }
}

3 个答案:

答案 0 :(得分:0)

假设"0" : "12 AM", "3" : "1 AM", "6" : "2 AM", and "8" : "3 AM"对已修复,以下将创建NSDictionary,您可以将其转换为所需格式的JSON:

NSDictionary *hoursDict = @{@"0" : @"12am",
                            @"3" : @"1am",
                            @"6" : @"2am",
                            @"8" : @"3am"};

NSDictionary *hoursForDaysDict = @{@"Sunday" : hoursDict,
                                   @"Monday" : hoursDict,
                                   @"Tuesday" : hoursDict,
                                   @"Wednesday" : hoursDict,
                                   @"Thursday" : hoursDict};

答案 1 :(得分:0)

试试这个

说你有3个阵列

NSArray *values = @[@"0",@"3",@"6",@"8"];
NSArray *times = @[@"12am",@"1am",@"2am",@"3am"];
NSArray *days = @[@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday"];

然后

NSMutableDictionary *finalDict = [[NSMutableDictionary alloc] init];

for(NSString *day in days) {

    NSMutableArray *valuePairArray = [[NSMutableArray alloc] init];
    NSInteger count = 0;
    for (NSString *value in values) {

        if ([times count] > count) {

            NSDictionary *dataDict = @{value: times[count]};
            [valuePairArray addObject:dataDict];

        }
        count++;
    }
    [finalDict setObject:valuePairArray forKey:day];
}

NSLog(@"%@", finalDict);

现在将此词典转换为JSON

答案 2 :(得分:0)

这将帮助您在所需的结构中填充字典:

NSArray *finalJSONArray = @[@"0",@"3",@"6",@"8"];
NSArray *timeArray      = @[@"12am",@"1am",@"2am",@"3am"];
NSArray *dayArray       = @[@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday"];

NSMutableDictionary *resultDic = [@{} mutableCopy];
for (NSString *day in dayArray)
{
    resultDic[day] = [@{} mutableCopy];

    NSUInteger numOfObjects = MIN([finalJSONArray count], [timeArray count]);
    for (int i = 0; i < numOfObjects; i++)
    {
        resultDic[day][finalJSONArray[i]] = timeArray[i];
    }
}