2012-08-01 50 views
-1

我有json的问题。 我要做查询:JSON in objective-c

{ "method": "authorize", "params": [ "100000202", "TestApp677" ] }

到端点:HTTP://xzasddfe.com/authorize/版本= 2_01

怎么办呢?

+0

你们是不是要发送或从URL接收数据? – ilhnctn 2012-08-01 12:02:24

+1

使用'NSJSONSerialization'([documentation](http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html))。对于请求,您可以使用[ASIHTTPRequest](http://allseeing-i.com/ASIHTTPRequest/)。 – mAu 2012-08-01 12:02:25

+1

你可以发布你到目前为止尝试过的吗? – thegrinner 2012-08-01 12:05:05

回答

0

首先看看一些JSON库。我更喜欢JSONKit,因为它非常易于使用。

接下来选择一种方法将数据发布到您的端点(NSURLConnection,ASIHTTPRequest)。

这里使用JSONKit和NSURLConnection的一个例子:

-(void) postData { 
    NSURL *url=[NSURL URLWithString:@"http://xzasddfe.com/authorize/?ver=2_01"]; 

    NSMutableDictionary *dic=[NSMutableDictionary dictionary]; 
    [dic setValue:@"authorize" forKey:@"method"]; 
    [dic setVObject:[NSArray arrayWithObjects:@"100000202",@"TestApp677",nil] forKey:@"params"]; 

    NSString *jsonString=[dic JSONString]; 

    NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

    [request release]; 

}