2012-08-01 51 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]; 

}