在 ObjC 中工作,而不是在 swift 中工作。我在 swift 中使用 JSON 解析有什么问题

萨加尔

我只是想从 URL 解析 JSON,下面用 ObjC 编写的代码运行良好并给出了正确的响应,但是用 swift 3.1 编写的代码没有给出正确的响应。给我500 Internal Error

我在做什么错了。谁能指导一下。提前致谢。

我还尝试使用从 ObjC 到 Swift 的转换器。但是它失败了一些错误,虽然我解决了这些错误,但仍然没有给出正确的输出。

ObjC 代码如下。

-(void)comparedatainserturl

{
    NSError *error;
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];

    [dict setObject:@"e639e129d0f3ab7d" forKey:@"imei_no"];
    [dict setObject:@"2017-08-29 12:18:44" forKey:@"current_time"];
    [dict setObject:@"0" forKey:@"tbl_offer_details"];
    [dict setObject:@"0" forKey:@"tbl_card_type"];
    [dict setObject:@"0" forKey:@"tbl_dashboard"];
    [dict setObject:@"0" forKey:@"tbl_category_details"];
    [dict setObject:@"0" forKey:@"tbl_sub_category_details"];
    [dict setObject:@"0" forKey:@"tbl_payment_option"];
    [dict setObject:@"0" forKey:@"tbl_payment_option_provider"];
    [dict setObject:@"0" forKey:@"tbl_like_favorite_share"];
    [dict setObject:@"0" forKey:@"tbl_user_details"];

    [arr addObject:dict];

    NSLog(@"arra :- %@",[arr description]);

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSString *string = [NSString stringWithFormat:@"data=%@",jsonString];

    NSLog(@"jsonData as string:\n%@", string);

    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

    NSString *urlString=[NSString stringWithFormat:@"%@",@"http://admin.scontos.com/index.php/Android_api/Offers_download"];

    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod: @"POST"];

    [request setHTTPBody: data];

    NSError *err;

    NSURLResponse *response;

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

    NSLog(@"gotresponsestring=%@", resSrt);
}

下面的Swift代码,

func  comparedatainserturl() {


        var arrayParams : NSDictionary = [:]

        arrayParams  = ["imei_no":"e639e129d0f3ab7d","current_time":"2017-08-2911:08:26","tbl_offer_details":"0","tbl_card_type":"0","tbl_dashboard":"0","tbl_category_details":"0","tbl_sub_category_details":"0","tbl_payment_option":"0","tbl_payment_option_provider":"0","tbl_like_favorite_share":"0","tbl_user_details":"0"]

        print(arrayParams)

        let valueArr : Array = [arrayParams]
        print(valueArr)

        let jsonData: Data? = try! JSONSerialization.data(withJSONObject: valueArr, options: .prettyPrinted)

        let jsonString = String(data: jsonData!, encoding: String.Encoding.utf8)
        let string: String = "data=\(jsonString)"
        print("jsonData as string:\n\(string)")
        let dataa: Data? = string.data(using: String.Encoding.utf8)

        var _: NSError?
        let url1:URL = URL(string:"http://admin.scontos.com/index.php/Android_api/Offers_download")!
        let request:NSMutableURLRequest = NSMutableURLRequest(url: url1)
        request.httpMethod = "POST"

        request.httpBody = dataa

        var response: URLResponse?

        do{
            let urlData: Data? = try NSURLConnection.sendSynchronousRequest(request as URLRequest, returning:&response)


            let resstr = NSString(data: urlData!, encoding: String.Encoding.utf8.rawValue)
            print("Response is \(resstr! as String)")

        }catch{
            print(error)
        }

    }
罗希特·马格杜姆

在您的 swift 代码中,您将附加以下字符串。

let jsonString = String(data: jsonData!, encoding: String.Encoding.utf8)/在下面的行中,您将带有可选值的 JsonString 附加到带有此“()”的可选关键字。所以你需要解开可选值/

let string: String = "data=\(jsonString!)"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章