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?
Converting to JSON You can use jsonStringor jsonDatato get the NSStringor NSDataencoded versions in JSON respectively. NSData *jsonData = [d jsonData]; Both methods are available on NSNull , NSNumber , NSArray , NSDictionary , NSObject , and NSString .
You do not need to escape colons. It's really only quotes that ever need to be escaped in JSON.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With