how to create json in objective-c

房东的猫 提交于 2019-11-30 08:20:07
bryanmac

You're missing this line to convert it to json

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:setUser 
                   options:NSJSONWritingPrettyPrinted error:&error];

Here's a tutorial on NSJSONSerialization that may help you: http://www.raywenderlich.com/5492/working-with-json-in-ios-5

After that, you can convert the NSData to an NSString to print:

Convert UTF-8 encoded NSData to NSString

You can try following to create JSON:

NSArray *objects=[[NSArray alloc]initWithObjects:objects here,nil];
NSArray *keys=[[NSArray alloc]initWithObjects:corresponding keys of objects,nil];
NSDictionary *dict=[NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSData *jsonData=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

this worked perfectly in my case

Hardik Mamtora

Try below

NSDictionary *o1 = [NSDictionary dictionaryWithObjectsAndKeys:
@"ABCD", @"key1",
@"EFG", @"key2",
nil];

NSDictionary *o2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"XYZ", @"key1",
@"POI", @"key2",
nil];

NSArray *array = [NSArray arrayWithObjects:o1, o2, nil];

NSString *jsonString = [array JSONRepresentation];

// send jsonString to the server After executing the code above, jsonString contains:

[
    {
        "key1": "ABCD",
        "key2": "EFG"
    },
    {
        "key1": "XYZ",
        "key2": "POI"
    }
]

Try this

NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://api.iospond.com/api/index.php/GetData"]];
    NSError *error=nil;
    id response=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"Your JSON Object: %@ Or Error is: %@", response, error);
NSDictionary *jsonObject = @{
                                 @"a":@[
                @{
                                             @"title1”:@“AA”,
                                             @"title2” : @“BB”,
                                             @"subcats" : @[
                                                     @{
                                                         @"title1” : @“CC”,
                                                         @"title2” :@“DD”
                                                         }

                                                     ]

                                             }
                                         ]



                                 };

NSMutableString *mutableString = nil; NSString *string= @"";

@try
{
    if (mutableString == nil)
    {
        mutableString = [[NSMutableString alloc] init];
    }

    [mutableString appendFormat:@"{"];
    [mutableString appendFormat:@"\"string1\":%@"",",@""];
    [mutableString appendFormat:@"\"string2\":\"%@\"",@""];
    [mutableString appendFormat:@"}"];
    jsonString = mutableString ;
}
@catch (NSException *exception)
{

}
@finally
{
    return string;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!