gpt4 book ai didi

ios - 无法将 NSDictionary 值转换为 json

转载 作者:行者123 更新时间:2023-11-28 18:34:28 25 4
gpt4 key购买 nike

我有 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

关于ios - 无法将 NSDictionary 值转换为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21756925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com