how to create json in objective-c

后端 未结 6 1334
眼角桃花
眼角桃花 2020-12-31 04:19
NSData* jsonDataToSendTheServer;

NSDictionary *setUser = [NSDictionary
            dictionaryWithObjectsAndKeys:[@\"u\" stringByAppendingString:my.id],@\"id\",
             


        
相关标签:
6条回答
  • 2020-12-31 04:48
    NSDictionary *jsonObject = @{
                                     @"a":@[
                    @{
                                                 @"title1”:@“AA”,
                                                 @"title2” : @“BB”,
                                                 @"subcats" : @[
                                                         @{
                                                             @"title1” : @“CC”,
                                                             @"title2” :@“DD”
                                                             }
    
                                                         ]
    
                                                 }
                                             ]
    
    
    
                                     };
    
    0 讨论(0)
  • 2020-12-31 04:51

    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

    0 讨论(0)
  • 2020-12-31 04:54

    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"
        }
    ]
    
    0 讨论(0)
  • 2020-12-31 04:59

    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;
    }
    
    0 讨论(0)
  • 2020-12-31 05:00

    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);
    
    0 讨论(0)
  • 2020-12-31 05:04

    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

    0 讨论(0)
提交回复
热议问题