Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法将NSDictionary值转换为json_Ios_Objective C_Json_Nsdictionary_Nsjsonserialization - Fatal编程技术网

Ios 无法将NSDictionary值转换为json

Ios 无法将NSDictionary值转换为json,ios,objective-c,json,nsdictionary,nsjsonserialization,Ios,Objective C,Json,Nsdictionary,Nsjsonserialization,我想将NSDictionary值转换为Json字符串,如: { body = "10-feb"; comments = ( ); complete = 1; "created_at" = "2014-02-09T15:56:01Z"; "due_at" = "2014-01-10 20:00:00 +0000"; "due_on" = "2014-10-02"; id = 439824; "notification_

我想将NSDictionary值转换为Json字符串,如:

{
    body = "10-feb";
    comments =     (
    );
    complete = 1;
    "created_at" = "2014-02-09T15:56:01Z";
    "due_at" = "2014-01-10 20:00:00 +0000";
    "due_on" = "2014-10-02";
    id = 439824;
    "notification_sent" = 0;
    "notify_user" = 0;
    "organization_id" = 972;
    participants =     (
    );
    reminders =     (
    );
    starred = 0;
    "updated_at" = "2014-02-09T15:56:01Z";
    "user_id" = 11129;
}
我将其转换为json的方式是:

- (NSString*) jsonStringWithPrettyPrint:(BOOL) prettyPrint str:(NSDictionary *)str {
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:str
                                                       options:(NSJSONWritingOptions)    (prettyPrint ? NSJSONWritingPrettyPrinted : 0)
                                                         error:&error];

    if (! jsonData) {
        NSLog(@"jsonStringWithPrettyPrint: error: %@", error.localizedDescription);
        return @"{}";
    } else {
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
}

我不知道为什么它总是无法转换。

传递到
NSJSONSerialization
的对象只能包含
NSDictionary
NSArray
NSString
NSNumber
NSNull


NSDate
对象必须首先使用
NSDateFormatter
显式转换为
NSStrings
NSJSONSerialization
中传递的
对象只能包含
NSDictionary
NSString
NSNumber
NSNull


NSDate
必须首先使用
NSDateFormatter
将对象显式转换为
NSStrings
得到的错误是什么?是
NSDate
到期吗?是的,这是一个NSDate。我也添加了异常断点。它停止了,但我尝试按continue在控制台中获取堆栈跟踪但是没有日志,错误仍然为零。这是一个打字错误吗<代码>选项:(NSJSONWritingOptions)(预打印?NSJSONWRITING预打印:0)
我不相信是这样。您遇到了什么错误?是否在
NSDate
到期?是的,是NSDate。我也添加了异常断点。它停止了,但我尝试按continue在控制台中获取堆栈跟踪,但没有任何日志,错误仍然为零。这是打字错误吗<代码>选项:(NSJSONWritingOptions)(预打印?nsjsonwritingprettypted:0)
我不这么认为。使用
[nsstringstringwithformat:@“%@”,date]
将日期字符串转换为
NSString
,并且问题消失了:)您创建JSON数据的问题消失了。但是必须有人来处理数据。检查您的文档以了解他们的期望。顺便说一句,您可以更轻松地调用[myDate description]。使用
[NSString stringWithFormat:@“%@”,date]
将日期字符串转换为
NSString
,问题消失了:)您创建JSON数据的问题消失了。但是必须有人来处理数据。检查您的文档以了解他们的期望。顺便说一句,你可以更轻松地调用[myDate description]。