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 create a string.json , Please help if any one know. Thanks in advance.
try this code it may be useful for u......
Assuming that you already have the JSON in an
NSString
, you can save it to a file by calling:The documentation for this function is here.
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
.Now, use above data to create a string object.
I had to build something similar and applied what I did to your string - it may not be the best way to achieve the result, but it worked for me. For the inner survey responses array (in my case it was a different array), I simply chained several together.
Try this : if there are more then one item then put (str = ) in loop
just set dictionary for Data with JSON object like bellow
See also this link sending-string-as-json-object
i hope this helpful to you..