嗨,我正在尝试将NSMutable转换为JSON字符串,这是我的代码:
-(NSString *) buildFeedback {
sqlite3_stmt *statement;
sqlite3 *notificationDB;
NSString *result = @"";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
NSString *dbPathString = [docPath stringByAppendingPathComponent:@"feedbacks.db"];
if (sqlite3_open([dbPathString UTF8String], ¬ificationDB)==SQLITE_OK) {
NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM FEEDBACKS"];
const char* query_sql = [querySql UTF8String];
if (sqlite3_prepare(notificationDB, query_sql, -1, &statement, NULL)==SQLITE_OK) {
while (sqlite3_step(statement)==SQLITE_ROW) {
NSString *feedbackText = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
NSMutableDictionary *feedback = [NSMutableDictionary dictionary];
[feedback setObject:feedbackText forKey:@"content"];
NSMutableDictionary *feedbacks = [NSMutableDictionary dictionary];
[feedbacks setObject:feedback forKey:@"feedback"];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:feedbacks
options:0
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
if ([result length])
result = [result stringByAppendingString:@","];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
result = [result stringByAppendingString:jsonString];
}
}
}
}
sqlite3_close(notificationDB);
return result;
}
在我的Rails服务器中,我得到了这个:
参数:{“feedbacks” =>“{\” feedback \“:{\” content \“:\” X <
我的问题是如何在不使用转义符(\“)的情况下执行真正的json字符串。
最佳答案
您的例程正在创建一个带有NSDictionary
键的feedback
(并且您将content
字典置于其下方),将其制成字符串,并将其附加到结果中。然后,您似乎正在接受该命令,并使用带有该字符串的feedbacks
键控的对象制作字典。
相反,您应该返回NSData
,并让while
循环将对象添加到NSMutableArray
,并在循环结束时对其进行编码。
我建议一些事情:
sqlite3
调用失败,请检查sqlite3_errmsg
。 sqlite3_finalize
。 sqlite3_close
块内移动if
。 while
循环。 NSMutableArray
代替NSMutableDictionary
作为feedbacks
(并将其声明移到while
循环之外)。 从而:
-(NSData *) buildFeedback
{
sqlite3_stmt *statement;
sqlite3 *notificationDB;
NSData *jsonData = nil;
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
NSString *dbPathString = [docPath stringByAppendingPathComponent:@"feedbacks.db"];
if (sqlite3_open([dbPathString UTF8String], ¬ificationDB)==SQLITE_OK) {
NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM FEEDBACKS"];
const char* query_sql = [querySql UTF8String];
if (sqlite3_prepare(notificationDB, query_sql, -1, &statement, NULL)==SQLITE_OK) {
NSMutableArray *feedbacks = [NSMutableArray array]; // move this outside the `for` loop and make array
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *feedbackText = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
NSMutableDictionary *feedback = [NSMutableDictionary dictionary];
[feedback setObject:feedbackText forKey:@"content"];
[feedbacks addObject:feedback];
}
// move the JSON stuff outside of the loop
NSError *error;
jsonData = [NSJSONSerialization dataWithJSONObject:feedbacks
options:0
error:&error];
if (!jsonData) {
NSLog(@"Got an error: %@", error);
} else {
// if you want to look at this for diagnostic purposes, that's fine,
// but this function should return the `NSData`
NSLog(@"jsonString = %@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
}
// don't forget to finalize
sqlite3_finalize(statement);
}
else
{
NSLog (@"%s: prepare failed: %s", __FUNCTION__, sqlite3_errmsg(notificationDB));
}
// this should go inside the "open" == SQLITE_OK block
sqlite3_close(notificationDB);
}
else
{
NSLog(@"%s: sqlite3_open failed!", __FUNCTION__);
}
return jsonData;
}
关于ios - Json不正确\“iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16859787/