Two ways to pass JSON data to the server

Source: Internet
Author: User

When it comes to transmitting JSON data to the server, how can I upload the parameters to the server in JSON form? Here's how to get the JSON string. There are a total of two ways to get the JSON string. Of course, uploading to the server, cannot pass the string, is to turn into NSData, that is, the binary form. This is not discussed here, just say, how to get the JSON string.

- (void) viewdidload{[Super Viewdidload]; //Do any additional setup after loading the view. //The first method is to combine the parameters to be passed into an array, and then convert the dictionary to a string by Jsonkitnsdictionary* jsondic = [Nsdictionary Dictionarywithobjectsandkeys:@"Li Hua",@"name",@"female",@"Sex",@" -",@" Age", nil]; NSString*jsonstring=[Jsondic jsonstring]; NSLog (@"---jsonstring----%@", jsonstring); //The second method is to directly combine strings in JSON form. This method is suitable for the comparison of the order of the parameters with special requirements. Otherwise, it looks a little cumbersome. NSString *jsonstringother=[nsstring stringWithFormat:@"{\"Name\": \ "%@\", \ "sex\": \ "female \", \ "age\": \ "20\"}",@"Li Hua"]; NSLog (@"-----jsonstringother---%@", Jsonstringother); }

Printing results:

 the- .- at  -: -:58.765Two ways to pass JSON data to the server [13692: 60b]---jsonstring----{"Sex":"female","name":"Li Hua"," Age":" -"} the- .- at  -: -:58.767Two ways to pass JSON data to the server [13692: 60b]-----jsonstringother---{"name":"Li Hua","Sex":"female"," Age":" -"}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.