• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 我如何将json字符串发布到服务器

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:32:01 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这是我必须发布的 json 字符串...

{
    "data": {
        "description": "",
        "current_value": "",
        "serialno": "",
        "condition": "",
        "category": "category",
        "purchase_value": "",
        "new_or_used": "",
        "gift_or_purchase": "",
        "image": ""
    },
    "subtype": "fd3102d8-bc19-424b-bca2-774a8fd7ea6f"
}

如何以 JSON 格式发布?



Best Answer-推荐答案


这个 Q 我们肯定是重复的,但这里是完整的示例代码,作为一个长例程。只需复制和粘贴即可。

首先设置 JSON...

-(void)sendTestJsonCommand
    {
    NSMutableDictionary *dict = @{
        @"heights""4_5_7",
        @"score""4",
        @"title""Some Title",
        @"textBody""Some Long Text",
        @"happy""y"
        }.mutableCopy;

    NSError *serr;

    NSData *jsonData = [NSJSONSerialization
        dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&serr];

    if (serr)
        {
        NSLog(@"Error generating json data for send dictionary...");
        NSLog(@"Error (%@), error: %@", dict, serr);
        return;
        }

    NSLog(@"Successfully generated JSON for send dictionary");
    NSLog(@"now sending this dictionary...\n%@\n\n\n", dict);

接下来,正确地将命令和 json 异步发送到您的服务器...

#define appService [NSURL \
  URLWithString"http://www.corp.com/apps/function/user/pass/id/etc"]

    // Create request object
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:appService];

    // Set method, body & content-type
    request.HTTPMethod = @"OST";
    request.HTTPBody = jsonData;
    [request setValue"application/json" forHTTPHeaderField"Content-Type"];
    [request setValue"application/json" forHTTPHeaderField"Accept"];

    [request setValue:
        [NSString stringWithFormat:@"%lu",
        (unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"];

    // you would almost certainly use MBProgressHUD at this point
    // to display some sort of spinner or similar action on the UX

最后,(A) 使用 NSURLConnection 正确连接,(B) 正确解释从服务器返回给您的信息。

    [NSURLConnection sendAsynchronousRequest:request
        queue:[NSOperationQueue mainQueue]
        completionHandler:^(NSURLResponse *r, NSData *data, NSError *error)
        {

        if (!data)
            {
            NSLog(@"No data returned from server, error ocurred: %@", error);
            NSString *userErrorText = [NSString stringWithFormat:
               @"Error communicating with server: %@", error.localizedDescription]
            return;
            }

        NSLog(@"got the NSData fine. here it is...\n%@\n", data);
        NSLog(@"next step, deserialising");

        NSError *deserr;
        NSDictionary *responseDict = [NSJSONSerialization
                                      JSONObjectWithData:data
                                      options:kNilOptions
                                      error:&deserr];

        NSLog(@"so, here's the responseDict\n\n\n%@\n\n\n", responseDict);

        // LOOK at that output on your console to learn how to parse it.
        // to get individual values example blah = responseDict[@"fieldName"];
        }];

    }

希望它可以节省一些人的打字时间!

关于ios - 我如何将json字符串发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26094270/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap