如何使用NSJSONSerialization转换此json?
{
"messages":{
"message":{
"user":"value",
"pass":"value",
"url":"value"
}
}
}
Run Code Online (Sandbox Code Playgroud) 我需要在iOS中处理任意结构的JSON数据.
但是,NSJSONSerialization将true/false值转换为NSNumber.
所以我的问题是,如何判断何时NSNumber从布尔值true/ 转换false为实际数字0/ 1?
注意:我知道[NSNumber numberWithBool],但在这里,我想弄清楚何时适合调用它.我也知道编写自己的解析器可以解决这个问题,但我想确保在完成该路由之前没有更简单的解决方案.
Apple的文档包含NSJSONWritingOptions枚举,其中定义了一个常量:NSJSONWritingPrettyPrinted,并说"如果未设置此选项,则会生成最紧凑的可能JSON表示."
我绝对想要最紧凑的JSON表示.
我的问题是:传递dataWithJSONObject的正确值是什么:options:error:function如果我不想打印漂亮,为什么?
NSJSONWritingOptions枚举定义为
enum { NSJSONWritingPrettyPrinted = (1UL << 0) }; typedef NSUInteger NSJSONWritingOptions;
Run Code Online (Sandbox Code Playgroud)
我在一篇不同的帖子中读过0应该通过,但没有进一步的解释为什么它不应该是零.
我已经为用户创建了模型类,如下所示.
public class SignUpUser {
public var fullName : String?
public var id : Int?
public var city : String?
public var email : String?
public var address : String?
public var lastName : String?
public var countryCode : String?
public var firstName : String?
public var zipCode : Int?
public var contactNumber : Int?
public var sex : String?
public var dob : String?
public var signupType : String?
public var verified : String?
public var emailTokenExpiration : String? …Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码。仅当 JSON 数据不以“[”字符开头时,它才有效。它适用于以“{”字符开头的 JSON 数据。这里有一个类似的问题:Parsing JSON array in swift但大多数方法已被弃用,我无法让代码工作。这是我正在使用的 JSON 调用:
guard let json = (try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any] else {
print("Error: Could not parse JSON!!!")
return
}
Run Code Online (Sandbox Code Playgroud)
我尝试删除所有选项并使用allowFragments 和 mutableLeaves 等。据我了解, mutableContainers 是默认设置,但我一直在尽我所能。任何帮助或建议将不胜感激。
以下是我正在使用的 JSON 数据的示例:
{ "CREATED_BY" = "域\用户"; "CREATED_DATE" = "2011-09-30T15:00:13"; 状态=U;“EMPLOYEE_NUMBER”= 039000292;"UPDATED_BY" = "域\用户""; "UPDATED_DATE" = "2014-08-02T13:22:01"; }
我有一个班级(PushNotificationSender)。该类拥有一个名为 的函数,sendPushNotification该函数接受FCM Token、Notification title、 和Notification body。鉴于我知道他们的FCM Token.
我的目标:使用此功能向多个用户发送相同的通知。
我尝试为每个用户调用它,FCM Token并且还尝试更改函数的参数以获取一组令牌,而不仅仅是一个,但还没有任何效果。关于如何完成升级有什么想法吗?
PushNotificationSender:
class PushNotificationSender {
init() {}
// MARK: Public Functions
public func sendPushNotification(to token: String, title: String, body: String, completion: @escaping () -> Void) {
let urlString = "https://fcm.googleapis.com/fcm/send"
let url = NSURL(string: urlString)!
let paramString: [String : Any] = ["to" : token,
"notification" : ["title" : title, "body" : body],
"data" : ["user" : …Run Code Online (Sandbox Code Playgroud) push-notification apple-push-notifications nsjsonserialization swift firebase-cloud-messaging
我试图构建一个基于文档的Cocoa应用程序,当我尝试解析readFromData: ofType: error:方法中的JSON时,我收到一个错误:fatal error: unexpectedly found nil while unwrapping an Optional value.导致错误的实际行如下:
override func readFromData(data: NSData?, ofType typeName: String?, error outError: NSErrorPointer) -> Bool {
var error: NSErrorPointer!
var loadedDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: error) as? NSDictionary // this causes an error
return true
}
Run Code Online (Sandbox Code Playgroud)
看起来该NSJSONSerialization.JSONObjectWithData()方法导致了错误,但为什么会发出错误呢?
我改变了options:说法要么nil或者0,也改变error:参数nil,但没有解决问题.
我也确保data不是nil.
那我错过了什么?我认为JSON方法试图在其中强行展开,但我怎么知道呢?我怎样才能逃脱致命错误并让我的应用程序正常运行?
我在Yosemite beta3上使用Xcode6-beta 3.
我有一个问题是将核心数据管理对象序列化为JSON对象.我正在使用Mantle这样做,这是相关的NSDate.我收到了这个错误.
'Invalid type in JSON write (__NSDate)'
Run Code Online (Sandbox Code Playgroud)
这就是抛出异常的地方.它是好的,直到代码行4(NSDictionary*jDict ....)
//NSManagedObject from Core Data
Memo *newMemo = [self fetchMemo:memo.uuid];
NSError *errorMantle;
//MTLModel model
BSMemo *memooo = [MTLManagedObjectAdapter modelOfClass:[BSMemo class] fromManagedObject:newMemo error:&errorMantle];
NSDictionary *jDict = [MTLJSONAdapter JSONDictionaryFromModel:memooo];
//Serialising using NSJSONSerialization
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jDict options:NSJSONWritingPrettyPrinted error:&error];
Run Code Online (Sandbox Code Playgroud)
这是怎么了格式化NSDate的MTLModel
+ (NSValueTransformer *)dateJSONTransformer {
static dispatch_once_t onceToken;
static NSDateFormatter *dateFormatter;
dispatch_once(&onceToken, ^{
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd'T'HH:mm:ssZZZ"];
});
return [MTLValueTransformer transformerWithBlock:^id(NSString *string) {
return [dateFormatter dateFromString:string];
}]; …Run Code Online (Sandbox Code Playgroud) core-data nsdateformatter ios nsjsonserialization github-mantle
我的iOS应用程序从REST服务获取JSON.JSON包含状态代码和数据.如果代码是200,则数据是请求的对象,否则数据是错误描述.
JSON可用于构建多种对象,因此我创建了一个工厂类,它在构造函数中获取json数据,实例方法根据需要将数据解析为不同的对象.
我的问题是,如果状态代码不是200,可以在构造函数中包含NSError**作为参数.方法头看起来像这样:
-(instancetype) initWithData:(NSData *) json error: (NSError **) error;
Run Code Online (Sandbox Code Playgroud) 请帮助我,我试图将NSString转换为JSON,从该字符串中获取特定数据.这是我的字符串,我尝试从NSString转换为JSON.该字符串被动态地提取到我在下面的字符串中粘贴的jsonString中.
NSString *jsonString = @"{status=success&unmappedstatus=captured&instrument_type=iOS&}";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
Run Code Online (Sandbox Code Playgroud)
结果将显示为null,我在NSLog中显示,甚至在UIalert视图中显示
NSLog(@"-->>JSON_data: %@",json);
Run Code Online (Sandbox Code Playgroud)
请帮帮我这个..