2015-05-08 55 views
-1

我有一個json字符串和另一個。我必須一起追加,發送到服務器並獲取用戶ID。我困在中間。附加的字符串不會轉換爲NSURL。iOS中的JSON轉換問題


這裏是我的代碼。

- (void)convertingstringtojsondata 
{ 
    NSArray *components = [[NSArray alloc]initWithObjects:ismunicipality,mobilenumberstring,placestring,namestring, nil]; 
    NSArray *keys = [[NSArray alloc]initWithObjects:@"is_municipality",@"ph_nbr",@"place",@"reg_name", nil]; 
    NSLog(@"%@",components); 

    NSDictionary *dict = [[NSDictionary alloc]initWithObjects:components forKeys:keys]; 

    NSLog(@"dict %@", dict); 

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict 
                 options:0 
                 error:nil]; 
    NSLog(@"JSON DATA %@", jsonData); 

    jsonData= jsonData; 

    JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding]; 

    NSLog(@"JSON STRING%@",JSONString); 
} 
-(void)sendjsonforregistration 
{ 
[self convertingstringtojsondata]; 

    registerwithserverstring = [[NSMutableString alloc]initWithString:@"http://forthepeople.mcms.net.in/forthepeople_service/registration?reg="]; 
    [registerwithserverstring appendString:JSONString]; 

    NSLog(@" appended string %@", registerwithserverstring); 

    NSURL *registerURL = [NSURL URLWithString:registerwithserverstring]; 

    NSLog(@"register URL%@", registerURL); 

NSMutableURLRequest * request = [[NSMutableURLRequest alloc]initWithURL:registerURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:0]; 
NSOperationQueue * queue = [[NSOperationQueue alloc]init]; 
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * response, NSData * data, NSError * error) { 
    NSData * jsonData1 = [NSData dataWithContentsOfURL:registerURL]; 

    NSLog(@"%@", jsonData1); 

    dataDictionary1 = [NSJSONSerialization JSONObjectWithData:jsonData1 options:0 error:&error]; 

    NSLog(@"DATA DICTIONARY %@", dataDictionary1); 

}]; 
} 

問題: - 的REGISTERURL顯示空值...

+0

一個長時間的調試我得到了最後的字符串,具體如下:http://forthepeople.mcms.net.in/forthepeople_service/registration?reg={"reg_name 「:」 ugh「,」place「:」Malappuram「,」ph_nbr「:」7204412649「,」is_municipality「:」1「}。但是這個沒有被轉換成NSURL。 –

+0

hey Alvin,你能告訴我你是否想用GET或POST類型發送數據。爲什麼要在JSON中編碼Get查詢字符串? –

+0

您可以發佈此請求的正面回覆,同時註冊成功嗎? –

回答

-1

我會建議使用NSURLComponents來構建你的URL,它正確地處理查詢參數編碼在NSURLQueryItem是你的提供給它。

-1

我認爲你需要編碼NSString之前轉換爲NSURl。你可以用這個做。

NSMutableString* registerwithserverstring = [[NSMutableString alloc]initWithString:@"http://forthepeople.mcms.net.in/forthepeople_service/registration/reg=?{\"reg_name‌​\":\"ugh\",\"place\":\"Malappuram\",\"ph_nbr\":\"7204412649\",\"is_municipality\":\"1\"}"]; 

NSString * properlyEncodedString = [registerwithserverstring stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; 

NSURL *registerURL = [NSURL URLWithString:properlyEncodedString]; 

NSLog(@"register URL%@", registerURL); 

輸出:

Printing description of registerURL: 
http://forthepeople.mcms.net.in/forthepeople_service/registration/reg=?%7B%22reg_name%E2%80%8C%E2%80%8B%22:%22ugh%22,%22place%22:%22Malappuram%22,%22ph_nbr%22:%227204412649%22,%22is_municipality%22:%221%22%7D 
+0

@alvin G,讓我知道你是否還有什麼問題 –

+0

這裏有什麼新東西,甚至我的回答也說明你說了什麼。 – Vizllx

+0

@Vizllx,實際上你說'NSURL URLWithString:'是錯誤的,但在轉換爲nsurl之前只需要編碼就沒有錯。 –