Reputation: 91
My Json Response:
{
"xxx": {
"y": {
"id": 1,
"name": "z"
},
"startday": "2016-01-10",
"status": "New",
"total": 1,
"a": [
{
"id": 766,
"b": {
"id": 3,
"name": "c"
},
"d": {
"id": 4
},
"e": {
"id": 1,
"name": "f"
},
"g": {
"id": 8,
"name": "h"
},
"hours": 1,
"comments": "",
"spent_on": "2016-01-10"
}
]
}
}
From this i created a string like this:
NSString * str = @"{\"xxx\":{\"y\":{\"id\":1,\"name\":\"z\"},\"startday\":\"2016-01-10\",\"status\":\"New\",\"total\":1.0,\"a\":[{\"id\":766,\"b\":{\"id\\":3,\\"name\\":\\"c\\"},\\"d\\":{\\"id\\":4},\\"e\\":{\\"id\\":1,\\"name\\":\\"f\\"},\\"g\\":{\\"id\\":8,\\"name\\":\\"h\\"},\\"hours\\":1.0,\\"comments\\":\\"\\",\\"spent_on\\":\\"2016-01-10\\"}]}}";
Then I post this value by below code:
NSData *objectData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError];
if (jsonError == NULL)
{
msgView =@"error msg null";
NSURL *url = [NSURL URLWithString:@"xxx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData *requestData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: requestData];
}
It hits server and gets the response.similarly, I get all the values user entering the app.I create the NSstring with concatenation.
NSString* concate = [NSString stringWithFormat:@"%@%@%@%@,%@%@%@,%@,%@%@,%@%,%@,%@,%@%@%@%@%@%@,%@%@,%@%@,%@%@",xx,....];
so i get output response like this after concatenate:
{\"x\":{\"y\":{\"id\":1,
\"name\":\"a\"
},
\"startday\":\"2016-01-10\"
,
\"status\":\"New\"
,{\"total\":1,\"time_entries\":[{\"id\":766,
\"b\":{
\"id\":8,
\"name\":\"c\"
}
,
\"u\":{
\"id\":22
}
,\"d\":{\"id\":1
\"name\":\"e\"
},
\"l\":{
\"id\":8,
\"name\":\"g\"
}
,\"hours\":1,\"comments\":\"\",\"spent_on\":\"2016-01-12\"}]}}
So if pass this, it throws error.here i have to concatenate or combined different strings with other approach.or is there any possibilities to convert string to json response format.or else any other simple way to do this task.please suggest me ideas.
Upvotes: 0
Views: 509
Reputation: 27050
First you will need to convert your NSString
to NSData
by doing the following
NSData *data = [concate dataUsingEncoding:NSUTF8StringEncoding];
then simply use the JSONObjectWithData:
method of NSJSONSerialization
to convert it to JSON
NSError *error = nil;
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if(!error) {
NSLog(@"%@", json);
}
Upvotes: 4
Reputation: 687
First you need to convert you NSString
to NSData
NSString * str = @"{\"xxx\":{\"y\":{\"id\":1,\"name\":\"z\"},\"startday\":\"2016-01-10\",\"status\":\"New\",\"total\":1.0,\"a\":[{\"id\":766,\"b\":{\"id\\":3,\\"name\\":\\"c\\"},\\"d\\":{\\"id\\":4},\\"e\\":{\\"id\\":1,\\"name\\":\\"f\\"},\\"g\\":{\\"id\\":8,\\"name\\":\\"h\\"},\\"hours\\":1.0,\\"comments\\":\\"\\",\\"spent_on\\":\\"2016-01-10\\"}]}}";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
Now, this NSData
is converted to id
using NSJSONSerialization
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
Here, you have your json data
NSLog(@"%@",json);
Upvotes: 0