0

我的编程能力并不强,而且似乎面临着非常简单的问题。我不知道如何通过带有 json 的 REST 请求在服务器上发布数据。必须接受的格式是

{
    "date": "2012-12-20 12:00:00",
    "address": "asd",
    "name": "asdasdasdasdasd",
    "shipping_date": "2012-12-20 12:00:00",
    "receiver_phone": "123456789",
    "customer_phone": "123456789",
    "total_price": "1234",
    "items": {
        "1": {
            "item_id": "1",
            "quantity": "1",
            "type": "1",
            "color_id": "0"
        }
    }
} 

我发现一篇文章Post data in Objective C using Json但无法正确格式化我的字符串。

4

3 回答 3

0

看起来那篇文章正是你想要的,减去了格式化位。JSONKit是一个非常简单易用的类。我觉得它比NSJSONSerialization. 但为此,我将缝合NSJSONSerialization

// create a dictionary with the structure you want
NSDictionary *dict = @{
    @"date":@"2012-12-20 12:00:00",
    @"address":@"asd",
    @"name":@"asdasdasdasdasd",
    @"shipping_date":@"2012-12-20 12:00:00",
    @"receiver_phone":@"123456789",
    @"customer_phone":@"123456789",
    @"total_price": @"1234",
    @"items": @{
        @"1": @{
            @"item_id": @"1",
            @"quantity": @"1",
            @"type": @"1",
            @"color_id": @"0"
        }
    }
};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:nil error:&error];

if ( error ) {
    // handle accordingly
}
else if ( jsonData != nil ) {
    NSString *jsonRequest = [[NSString alloc] initWithData:jsonData encoding:NSUTF*Encoding];
    // build request like example
}

现在您可以使用示例的其余部分。

更新

我发现JSONKit比它强大得多NSJSONSerialization并且需要更少的步骤,但NSJSONSerialization应该产生相同的结果,并且不需要担心混合 ARC 和非 ARC 代码。

更新 2

我建议JSONKit认真看一下,它不那么挑剔(即值可以是 anNSNumber而不是NSString)。它确实需要-fno-objc-arc在构建阶段为JSONKit.m.

于 2013-03-04T17:05:02.697 回答
0

如果您创建一个字典然后使用 NSJSONSerialization 将其转换为数据会更好。

NSDictionary *params = ...

NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:params 
                                                   options:0 
                                                     error:&error];
[request setHTTPBody:postData];
于 2013-03-04T17:10:07.990 回答
0
- (IBAction)registerclick:(id)sender

{

if (_password.text==_repassword.text)
{
    [_errorlbl setHidden:YES];


NSString *requstUrl=[NSString stringWithFormat:@"http://iroidtech.com/fresery/index.php?route=api/fresery/registerCustomer"];

 NSString *postString=[NSString stringWithFormat:@"name=asd&email=sooraj&phonenumber=8111&password=soorajsnr&type=1&facebookid=&image_path="];
   // _name.text,_email.text,_mobile.text,_password.text

NSData *returnData=[[NSData alloc]init];

NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:requstUrl]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];


returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
resp=[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];


    c=[[resp valueForKey:@"status" ]objectAtIndex:0];
    b=[[resp valueForKey:@"message"]objectAtIndex:0];
于 2018-06-23T15:18:51.143 回答