如何在objective-c中创建json

时间:2012-08-13 10:52:30

标签: ios objective-c json nsjsonserialization

NSData* jsonDataToSendTheServer;

NSDictionary *setUser = [NSDictionary
            dictionaryWithObjectsAndKeys:[@"u" stringByAppendingString:my.id],@"id",
                                        @"GET_USER_INFO",@"command",
                                        @"",@"value",
                                        nil];

 NSLog(@"%@", jsonDataToSendTheServer);

这是我的代码。当我运行上面的代码时,我得到了这个打印

<7b226964 223a2275 35383738 37373334 31222c22 636f6d6d 616e6422 3a224745 545f5553 45525f49 4e464f22 2c227661 6c756522 3a22227d>

我不知道我是否可以创建一个json。

我该如何解决这个问题?

6 个答案:

答案 0 :(得分:31)

您缺少此行以将其转换为json

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

这是NSJSONSerialization的教程,可以帮助您: http://www.raywenderlich.com/5492/working-with-json-in-ios-5

之后,您可以将NSData转换为NSString进行打印:

Convert UTF-8 encoded NSData to NSString

答案 1 :(得分:7)

您可以尝试以下创建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];

这在我的案例中完美无缺

答案 2 :(得分:3)

尝试以下

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

//将jsonString发送到服务器 执行上面的代码后,jsonString包含:

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

答案 3 :(得分:0)

试试这个

<div id="div" style="background-image: url(http://kenwheeler.github.io/slick/img/lazyfonz2.png)"></div>

答案 4 :(得分:0)

NSDictionary *jsonObject = @{
                                 @"a":@[
                @{
                                             @"title1”:@“AA”,
                                             @"title2” : @“BB”,
                                             @"subcats" : @[
                                                     @{
                                                         @"title1” : @“CC”,
                                                         @"title2” :@“DD”
                                                         }

                                                     ]

                                             }
                                         ]



                                 };

答案 5 :(得分:-2)

NSMutableString * mutableString = nil;     NSString * string = @&#34;&#34 ;;

@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;
}