18
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。

我怎样才能解决这个问题?

4

6 回答 6

34

您缺少将其转换为 json 的这一行

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

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

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

将 UTF-8 编码的 NSData 转换为 NSString

于 2012-08-13T11:00:27.203 回答
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];

这在我的情况下非常有效

于 2012-08-13T11:01:20.703 回答
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"
    }
]
于 2014-04-30T03:31:04.920 回答
0

试试这个

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);
于 2017-05-08T18:48:19.693 回答
-2

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;
}
于 2015-11-27T02:31:53.700 回答
-2
NSDictionary *jsonObject = @{
                                 @"a":@[
                @{
                                             @"title1”:@“AA”,
                                             @"title2” : @“BB”,
                                             @"subcats" : @[
                                                     @{
                                                         @"title1” : @“CC”,
                                                         @"title2” :@“DD”
                                                         }

                                                     ]

                                             }
                                         ]



                                 };
于 2018-08-28T07:25:14.923 回答