【发布时间】:2011-05-18 04:12:16
【问题描述】:
【问题讨论】:
标签: ios objective-c json ios4 iphone-sdk-3.0
【问题讨论】:
标签: ios objective-c json ios4 iphone-sdk-3.0
创建一个对象数组或字典,表示您希望通过 JSON 发送的信息。完成后,将-JSONRepresentation 发送到数组/字典。该方法返回一个 JSON 字符串,然后您将其发送到服务器。
例如:
NSDictionary *o1 = [NSDictionary dictionaryWithObjectsAndKeys:
@"some value", @"key1",
@"another value", @"key2",
nil];
NSDictionary *o2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"yet another value", @"key1",
@"some other value", @"key2",
nil];
NSArray *array = [NSArray arrayWithObjects:o1, o2, nil];
NSString *jsonString = [array JSONRepresentation];
// send jsonString to the server
执行上述代码后,jsonString 包含:
[
{
"key1": "some value",
"key2": "another value"
},
{
"key1": "yet another value",
"key2": "some other value"
}
]
【讨论】:
创建一个 NSMutableDictionary 或 NSMutableArray 并用 NSNumbers 和 NSStrings 填充它。调用[<myObject> JSONRepresentation]返回一个JSON字符串。
例如:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"Sam" forKey:@"name"];
[dict setObject:[NSNumber numberWithInt:50000] forKey:@"reputation"];
NSString *jsonString = [dict JSONRepresentation];
【讨论】: