使用http get request我确实得到了这个json字符串:
hello
[
{
"upid":"1",
"uptitle":"test",
"uptype":"test",
"upsize":"1234",
"upname":"test",
"upuid":"1",
"uplocation":"1",
"uptimestamp":"2013-04-11 09:25:40"
},
{
"upid":"17",
"uptitle":"test",
"uptype":"video\/mov",
"upsize":"2232",
"upname":"testing",
"upuid":"1",
"uplocation":"",
"uptimestamp":"2013-04-12 11:47:20"
}
]
我如何以格式获得输出:
upid:1
uptitle:test
uptype:test
upsize:1234
upname:test
upuid:1
uplocation:1
uptimestamp:2013-04-11 09:25:40
upid:17
uptitle:test
uptype:video\/mov
upsize:2232
upname:testing
upuid:1
uplocation:
uptimestamp:2013-04-12 11:47:20
这是我的代码:
NSMutableURLRequest *GETrequest = [[NSMutableURLRequest alloc] init];
[GETrequest setURL:[NSURL URLWithString:@"http:someip/upload/?id=1&command=alluseruploads&uid=1"]];
[GETrequest setHTTPMethod:@"GET"];
[GETrequest setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
// And to send and receive an answer, use a NSURLResponse:
NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:GETrequest returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply length] encoding: NSASCIIStringEncoding];
NSLog(@"Reply: %@", theReply);
// converting string to data
NSData *jsonData = [theReply dataUsingEncoding:NSUTF8StringEncoding];
//NSLog(@"dic = %@", jsonData);
NSError* error;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@", jsonObject);
答案 0 :(得分:11)
您不必导入任何框架,因为它使用核心iOS框架。
如果您有一个名为NSString
的{{1}},那么您可以执行以下操作...
jsonString
这会将您的对象注销到控制台。它将是NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
// you can skip this step by just using the NSData that you get from the http request instead of converting it to a string.
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error:&localError];
NSLog(@"%@", jsonObject);
或NSArray
,并且将包含...
NSDictionary
然后,您可以迭代此对象以获取数据。
好的,使用你的代码......
NSDate
NSDictionary
NSArray
NSNumber
NSString
这应该创建你的对象。您不需要转换为字符串,然后返回数据。
答案 1 :(得分:-1)
使用SBJson库。很容易。