如何在objective-c中创建json ?

时间:2022-09-07 11:16:41
NSData* jsonDataToSendTheServer;

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


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

 NSLog(@"%@", jsonDataToSendTheServer);

Here is my code. When I run above code I get this print

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

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

I don't have any idea whether I can created a json or not.

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

How can I fix this?

我该怎么解决这个问题呢?

5 个解决方案

#1


28  

You're missing this line to convert it to json

要将其转换为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

这里有一个关于NSJSONSerialization的教程,可以帮助您:http://www.raywenderlich.com/5492/working with-json-in- io5

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

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

Convert UTF-8 encoded NSData to NSString

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

#2


6  

You can try following to create JSON:

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

这在我的例子中非常有效

#3


3  

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:

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

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

#4


0  

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

#5


-2  

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

NSMutableString * mutableString =零;NSString字符串* = @ ";

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

#1


28  

You're missing this line to convert it to json

要将其转换为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

这里有一个关于NSJSONSerialization的教程,可以帮助您:http://www.raywenderlich.com/5492/working with-json-in- io5

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

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

Convert UTF-8 encoded NSData to NSString

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

#2


6  

You can try following to create JSON:

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

这在我的例子中非常有效

#3


3  

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:

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

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

#4


0  

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

#5


-2  

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

NSMutableString * mutableString =零;NSString字符串* = @ ";

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