How can I create a json string in objective C?
Solution 1
i totally agree with @Apurv, actually @Paras Joshi give the actual answer to your question...(how to send)
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:@"Survey1" forKey:@"surveyid"];
[dict setValue:@"responsetime" forKey:@"dd/mm/yyyy hh:mm:ss"];
.
.
.
and then an array forKey @"surveyresponses"
... in it again create a dictionary...bla..bla
Please make it clear first what you exactly want... How to send a JSON string / how to generate a JSON value.
learn to accept the answers as well as analyse the answers perfectly, and make your questions clear.
Solution 2
Set dictionary for Data with JSON object like below:
NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:yourDataDictionary options:NSJSONWritingPrettyPrinted error:&err];
NSLog(@"JSON = %@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
See also this link sending-string-as-json-object
Solution 3
If you already have JSON string, you can use it directly.
If you have an object(array and dictionary) and if you want to convert it into json string, use below method of NSJSONSerialization
.
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:@"Survey1" forKey:@"surveyid"];
//Add rest of the details in the dictionary
//Pass the dict in below method
+ (NSData *)dataWithJSONObject:(id)dict options:(NSJSONWritingOptions)opt error:(NSError **)error
Now, use above data to create a string object.
Solution 4
Sample code to generate Json String:
NSString *strdata =[NSString stringWithFormat:@"&data={\"user_id\":\"%@\",\"auth_token\":\"%@\",\"coupon_id\":\"%@\",\"rest_name\":\"%@\",\"name\":\"%@\",\"description\":\"%@\",\"condition\":\"%@\",\"coupon_code\":\"%@\",\"parent_menu_item\":\"%@\",\"discount_item\":\"%@\",\"discount_pecent\":\"%@\",\"start_date\":\"%@\",\"end_date\":\"%@\",\"status\":\"%@\"}",userid,auth_token,couponid,restuserid,txtCoupannm.text,txtvwCoupandesc.text,txtvwcoupancond.text,couponcode,appDelegate.tempparentmenuId,appDelegate.tempdiscountitemId,txtPercent.text,startdate.text,enddate.text,@"1"];
Solution 5
Try this : if there are more then one item then put (str = ) in loop
NSString *str = [NSString stringWithFormat:@"json_data={\"data\":["];
str = [str stringByAppendingString:[NSString stringWithFormat:@"{\"type\":\"%@\",\"surveyid\":\"%@\",\"veriety\":\"%@\",\"responsetime\":\"%@\",\"rate\":\"%@\",\"seeddepth\":\"%@\",\"groundspeed\":\"%@\",\"note\":\"%@\",\"AnhRate\":\"%@\",\"AnhVarRate\":\"%@\",\"WetRate\":\"%@\",\"WetVarRate\":\"%@\",\"WetType\":\"%@\",\"DryRate\":\"%@\",\"DryVarRate\":\"%@\",\"DryType\":\"%@\",\"MicroRate\":\"%@\",\"MicroVarRate\":\"%@\",\"MicroType\":\"%@\",\"NoteApp\":\"%@\",\"userid\":\"%@\",\"flid\":\"%d\",\"catid\":\"%d\",\"subcatId\":\"%d\",\"categoryname\":\"%@\",\"subcategoryname\":\"%@\",\"activitydate\":\"%@\",\"DateCreated\":\"%@\",\"DateUpdated\":\"%@\"},",Your variable
if ([str length] > 0) {
str =[str substringToIndex:[str length] - 1];
}
str = [str stringByAppendingString:@"]}"];
krishna
Updated on June 14, 2022Comments
-
krishna almost 2 years
I have to generate a json string dynamically and need to send to the server. Is any body know how to do it using
NSJSONSerialization
. Below is my string{ "surveyid": "Survey1", "responsetime": "dd/mm/yyyy hh:mm:ss", "location": null, "surveyresponses": [ { "questionid": "111", "responses": [ { "response": "Good", "optionid": 1, "language": "en" } ] }, { "questionid": "112", "responses": [ { "response": "bad", "optionid": 2, "language": "en" } ] } ] }
How can I create a string.json?
-
krishna about 11 yearsi need to generate a jsonstring like above from the scracth
-
krishna about 11 yearsi need to generate a jsonstring like above from the scracth
-
drewmm about 11 yearsWhat do you mean from scratch? Do you have the data in an
NSDictionary
object? If not, where are you getting the data from? -
Apurv about 11 yearsCreate a dictionary first. Add proper key values to it and then use above function.
-
krishna about 11 yearsI coudnt generate a proper dictionary for the above one that is the real pblm
-
Paras Joshi about 11 years@krishna see this link also.. stackoverflow.com/questions/11882696/create-a-jsonstring-in-ios
-
Volomike over 8 yearsThe best answer seems to be if we combine what you posted here, and then append ParasJoshi 's answer. One shows how to build the dictionary object, the other on how to make it into a string.
-
MRizwan33 about 6 yearswhat if i want to use just one string object.?