Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a json string in objective C?

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?

like image 906
krishna Avatar asked Mar 20 '13 06:03

krishna


People also ask

How to convert data to JSON in objective C?

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 .

Can JSON string contain colon?

You do not need to escape colons. It's really only quotes that ever need to be escaped in JSON.


1 Answers

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.

like image 106
Krish Allamraju Avatar answered Nov 15 '22 01:11

Krish Allamraju