-1

我对json有问题。我必须做查询:

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

到端点:http://xzasddfe.com/authorize/?ver=2_01

怎么做?

4

1 回答 1

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

}
于 2012-08-01T12:12:16.017 回答