我想将回复转换为NSDictionary
。响应是ALRegistrationResponse
(https://github.com/AppLozic/Applozic-Chat-iOS-Framework/blob/master/Applozic/DomainClasses/ALRegistrationResponse.m)的实现。
NSDictionary *_response = @
{
@"message":[response message] != nil ? [response message] : [NSNull null],
@"deviceKey":[response deviceKey] != nil ? [response deviceKey] : [NSNull null],
@"userKey":[response userKey] != nil ? [response userKey] : [NSNull null],
@"contactNumber":[response contactNumber] != nil ? [response contactNumber] : [NSNull null],
@"lastSyncTime":[response lastSyncTime] != nil ? [response lastSyncTime] : [NSNull null],
@"currentTimeStamp":[response currentTimeStamp] != nil ? [response currentTimeStamp] : [NSNull null],
@"brokerURL":[response brokerURL] != nil ? [response brokerURL] : [NSNull null],
@"imageLink":[response imageLink] != nil ? [response imageLink] : [NSNull null],
@"statusMessage":[response statusMessage] != nil ? [response statusMessage] : [NSNull null],
@"encryptionKey":[response encryptionKey] != nil ? [response encryptionKey] : [NSNull null],
@"displayName":[response displayName] != nil ? [response displayName] : [NSNull null],
@"notificationSoundFileName":[response notificationSoundFileName] != nil ? [response notificationSoundFileName] : [NSNull null]
};
答案 0 :(得分:1)
至少有点清洁的是以下语法:
NSDictionary *_response = @{
@"message": [response message] ?: [NSNull null],
@"deviceKey": [response deviceKey] ?: [NSNull null],
@"userKey": [response userKey] ?: [NSNull null],
@"contactNumber": [response contactNumber] ?: [NSNull null],
@"lastSyncTime": [response lastSyncTime] ?: [NSNull null],
@"currentTimeStamp": [response currentTimeStamp] ?: [NSNull null],
@"brokerURL": [response brokerURL] ?: [NSNull null],
@"imageLink": [response imageLink] ?: [NSNull null],
@"statusMessage": [response statusMessage] ?: [NSNull null],
@"encryptionKey": [response encryptionKey] ?: [NSNull null],
@"displayName": [response displayName] ?: [NSNull null],
@"notificationSoundFileName": [response notificationSoundFileName] ?: [NSNull null]
};
答案 1 :(得分:1)
另一种可能性是使用键作为选择器:
NSArray *keys = @[@"message", @"deviceKey", @"userKey", @"contactNumber", @"lastSyncTime", @"currentTimeStamp", @"brokerURL", @"imageLink", @"statusMessage", @"encryptionKey", @"displayName", @"notificationSoundFileName"];
NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
for (NSString *key in keys) {
SEL selector = NSSelectorFromString(key);
[dict setObject:[response performSelector: selector] ?: [NSNull null] forKey:key];
}
答案 2 :(得分:0)
创建一个检查nil的函数怎么样? 像这样:
try {
JSONObject jsonObject = new JSONObject(jsonQuery);
if (jsonObject != null){
JSONArray restaurants = jsonObject.getJSONArray("restaurants");
for (int i = 0; i < restaurants.length(); i++){
JSONObject restaurant = restaurants.getJSONObject(i);
JSONObject restaurantsJSONObject =restaurant.getJSONObject("restaurant");
if(restaurantsJSONObject.has("name")) {
String tempName = restaurantsJSONObject.getString("name");
Log.d(TAG, "Restaurant name: " + tempName);
}
}
}
} catch (JSONException e) {
Log.d(TAG, "Exception catched: " + e);
e.printStackTrace();
}