0

I have below snippet that I am supposed to create in objective-c.

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

Here's my attempt to create it

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];

I think I am close but for some reason, this is not working for me. I would appreciate any help. Thanks in advance.

2
  • 1
    [NSNumber numberWithInteger:terms] how can you do this ? terms is array not an integer Commented Nov 21, 2014 at 7:07
  • sorry that was supposed to be terms. Commented Nov 21, 2014 at 7:14

2 Answers 2

1

this should work

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]);

The Result

2014-11-21 15:03:14.396 Answering_question[23716:113224] result: {
    expression = "???=n1+n2+n3+n4";
    terms =     (
                {
            name = termsTitle;
            term =             (
                                {
                    name = answer;
                    value = "???";
                }
            );
        }
    );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @siu!!! This looks great and also it's a great example how I should be creating nested JSON in objective-c. Thanks again.
0

If you have some problems with the structure you need to write more detailed code. Like this:

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];

Output:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.