使用objective-c创建嵌套的json对象

时间:2014-11-21 06:49:02

标签: objective-c json

我有以下片段,我应该在objective-c中创建。

   {
  "expression": "???=n1+n2+n3+n4",
  "terms": [
    {
      "name": "termsTitle",
      "term": [
        {
          "name": "answer",
          "value": "???"
        }
      ]
    }
  ]
}

这是我创建它的尝试

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

NSArray *keysTerms = [[NSArray alloc] initWithObjects:@"name", @"term", nil];


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

NSArray *objectsTerms = [[NSArray alloc] initWithObjects:@"answer",term, nil];

NSDictionary *termsDict = [[NSMutableDictionary alloc] initWithObjects:objectsTerms forKeys:keysTerms];

[terms addObject:termsDict];

NSDictionary *statement = [[NSDictionary alloc]
                           initWithObjectsAndKeys:expression,@"expression",
                           terms,@"terms",
                           nil];

我认为我很接近,但出于某种原因,这对我不起作用。我将不胜感激任何帮助。提前谢谢。

2 个答案:

答案 0 :(得分:1)

这应该有效

NSDictionary* termDict = [NSDictionary dictionaryWithObjects:@[@"answer",@"???"] forKeys:@[@"name",@"value"]];

    NSArray* termArray = [NSArray arrayWithObjects:termDict, nil];

    NSDictionary* termsDict = [NSDictionary dictionaryWithObjects:@[@"termsTitle",termArray] forKeys:@[@"name",@"term"]];

    NSMutableArray* terms = [NSMutableArray arrayWithObjects:termsDict, nil];

    NSDictionary* result = [NSDictionary dictionaryWithObjects:@[@"???=n1+n2+n3+n4",terms] forKeys:@[@"expression",@"terms"]];

    NSLog(@"result: %@",[result description]);

结果

2014-11-21 15:03:14.396 Answering_question[23716:113224] result: {
    expression = "???=n1+n2+n3+n4";
    terms =     (
                {
            name = termsTitle;
            term =             (
                                {
                    name = answer;
                    value = "???";
                }
            );
        }
    );
}

答案 1 :(得分:0)

如果您对结构有一些问题,则需要编写更详细的代码。像这样:

NSMutableDictionary *mainDict= [NSMutableDictionary new];
[mainDict setObject:@"???=n1+n2+n3+n4" forKey:@"expression"];

NSMutableArray *terms = [NSMutableArray new];

NSMutableDictionary *term = [NSMutableDictionary new];
[term setObject:@"name" forKey:@"termsTitle"];

NSMutableArray *subTerms = [NSMutableArray new];
NSMutableDictionary *subTerm = [NSMutableDictionary new];
[subTerm setObject:@"answer" forKey:@"name"];
[subTerm setObject:@"???" forKey:@"value"];
[subTerms addObject:subTerm];

[term setObject:subTerms forKey:@"term"];

[terms addObject:term];

[mainDict setObject:terms forKey:@"terms"];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mainDict
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

输出:

{
  "expression" : "???=n1+n2+n3+n4",
  "terms" : [
    {
      "termsTitle" : "name",
      "term" : [
        {
          "name" : "answer",
          "value" : "???"
        }
      ]
    }
  ]
}