1

I want to create a JSON array of the following structure:

"Create Account":{ 
"RegistryNumber":"",
"People":[{
"PeopleId":"",
"email":"",
"pass":"",
}]
}

I am using the folllowing code to do it:

NSDictionary *content0 = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"PeopleId", @"0",
                              @"email", @"email",
                              @"pass", @"pass",nil];

  NSArray *peopledetails = [NSArray arrayWithObjects:content0,nil];
NSMutableDictionary *peopleDict = [[NSMutableDictionary alloc] init]; 
[peopleDict setObject:@"content0" forKey:@"People"];
  
NSMutableDictionary *details = [[NSMutableDictionary alloc] init];  
[details setObject:@"RegistryNumber" forKey:@"RegistryNumber"];
[details setObject:@"peopleDict" forKey:@"People"];
NSMutableDictionary *MainDict = [[NSMutableDictionary alloc] init]; 
[MainDict setObject:@"details" forKey:@"Create Account"];

But this gives me an error from the server. I have other API which works fine when array is not in picture.

3 Answers 3

1
- (void)simpleJsonParsing
{
    //  URL request with server
    NSHTTPURLResponse *response = nil;
    NSString *jsonUrlString = [NSString stringWithFormat:@"URL"];
    NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    //-- Get request and response though URL
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    //-- JSON Parsing
    NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"Result = %@",result);

    for (NSMutableDictionary *dic in result)
    {
         NSString *string = dic[@"Create Account"];
        if (string)
        {
             NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
             dic[@"Create Account"] = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        else
        {
             NSLog(@"Error in response");
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

1

change

[peopleDict setObject:@"content0" forKey:@"People"];

to

[peopleDict setObject:peopledetails forKey:@"People"];

Remove all @"" for objects.

@"details" is a string and details is an object

Or here is the simple way

  NSDictionary *dict=@{
                         @"Create Account": @{
                                 @"RegistryNumber": @"",
                                 @"People": @[
                                         @{
                                             @"PeopleId": @"",
                                             @"Email": @"",
                                             @"pass":@""
                                             }
                                         ]
                                 }
                         };

Comments

0

I think you have wrong structure

NSDictionary *content0 = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"PeopleId", @"0",
                              @"email", @"email",
                              @"pass", @"pass",nil];


postDict = @{@"Create Account":@{@"RegistryNumber":"","People":content0}}

now convert this into jason and send to your server

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.