2011-07-18 13 views

risposta

9

// data-> stringa

NSMutableDictionary *nameElements = [NSMutableDictionary dictionary];  

[nameElements setObject:@"abcd" forKey:@"username"]; 

[nameElements setObject:@"1234" forKey:@"password"];  

NSString* jsonString = [nameElements JSONString]; 

// String-> Dati

NSDictionary *nameElements_ = [jsonString objectFromJSONString]; 

for(NSString *key in [nameElements_ allKeys]) { 
    NSString* body = [nameElements_ objectForKey:key]; 
    NSLog(@"%@", body); 
} 
2

Questo è un esempio di invio di dizionario e array a server.che ha funzionato per me il 1000000%.

SBJSON *jparser = [[SBJSON new] autorelease]; 


NSString *ArrayjsonItems = [jparser stringWithObject:self.UrMergedArray]; 

NSString *DicjsonItems = [jparser stringWithObject:self.UrMergedDic]; 




NSLog(@"array Items :%@",self.UrMergedArray); 

NSLog(@"dic Items :%@",self.UrMergedDic); 




NSString *postString =[NSString stringWithFormat:@"Arrayitems=%@&Dicitems=%@",ArrayjsonItems,DicjsonItems]; 


NSLog(@"it is going to post : %@ \n\n",postString); 



NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:snapURL]; 

[request setHTTPMethod:@"POST"]; 

[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 



NSURLConnection *connection=[[NSURLConnection alloc] 
          initWithRequest:request 
          delegate:self]; 


if (connection) { 

    self.receivedData = [[NSMutableData data] retain]; 

} 


[connection release]; 
+0

questo dovrebbe essere SBJSON non JSONKit – Dilshan

+0

Dio benedica u !! Mi hai salvato .... –

+0

@HinataHyuga :) :) :) –

Problemi correlati