如何在ios中将nsstring格式转换为json响应格式?

如何在ios中将nsstring格式转换为json响应格式?,ios,objective-c,json,Ios,Objective C,Json,我的Json响应: { "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,

我的Json响应:

{
"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"
}
]
}
}
由此,我创建了如下字符串:

 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 *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];

}
它点击服务器并获得响应。类似地,我获取用户进入应用程序的所有值。我使用连接创建NSstring

NSString* concate = [NSString stringWithFormat:@"%@%@%@%@,%@%@%@,%@,%@%@,%@%,%@,%@,%@%@%@%@%@%@,%@%@,%@%@,%@%@",xx,....];
所以我在连接后得到如下输出响应:

{\"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\"}]}}

所以如果通过这个,,它抛出了错误。这里我必须用其他方法连接或组合不同的字符串。或者是否有可能将字符串转换为json响应格式。或者是否有其他简单的方法来完成此任务。请给我一些建议。

首先,您需要通过执行以下操作将
NSString
转换为
NSData

NSData *data = [concate dataUsingEncoding:NSUTF8StringEncoding];
然后简单地使用
NSJSONSerialization
JSONObjectWithData:
方法将其转换为JSON

NSError *error = nil;
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if(!error) {
    NSLog(@"%@", json);
}

首先,您需要通过执行以下操作将
NSString
转换为
NSData

NSData *data = [concate dataUsingEncoding:NSUTF8StringEncoding];
然后简单地使用
NSJSONSerialization
JSONObjectWithData:
方法将其转换为JSON

NSError *error = nil;
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if(!error) {
    NSLog(@"%@", json);
}

首先,您需要将
NSString
转换为
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];
现在,此
NSData
转换为使用
NSJSONSerialization

id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
这里有您的json数据

NSLog(@"%@",json);

首先,您需要将
NSString
转换为
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];
现在,此
NSData
转换为使用
NSJSONSerialization

id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
这里有您的json数据

NSLog(@"%@",json);