There will always be some nsnull types in the data returned from the backend, and when our handler crashes, we think of converting the Nsnull type in the returned data to an empty string of @ "". Here are the conversion methods:
1 Customization of several methods: put in the nsdictionary category
#pragma mark-Private method
Converts an item of type NULL in nsdictionary to @ ""
+ (Nsdictionary *) Nulldic: (Nsdictionary *) mydic
{
Nsarray *keyarr = [Mydic AllKeys];
Nsmutabledictionary *resdic = [[Nsmutabledictionary alloc]init];
for (int i = 0; i < keyarr.count; i + +)
{
ID obj = [mydic objectforkey:keyarr[i]];
obj = [self changetype:obj];
[Resdic setobject:obj forkey:keyarr[i];
}
return resdic;
}
Converts an item of type NULL in nsdictionary to @ ""
+ (Nsarray *) Nullarr: (Nsarray *) Myarr
{
Nsmutablearray *resarr = [[Nsmutablearray alloc] init];
for (int i = 0; i < myarr.count; i + +)
{
ID obj = Myarr[i];
obj = [self changetype:obj];
[Resarr Addobject:obj];
}
return Resarr;
}
Returns the original path of the NSString type
+ (NSString *) stringtostring: (NSString *) string
{
return string;
}
Convert items of type NULL to @ ""
+ (NSString *) nulltostring
{
return @ "";
}
#pragma mark-Public method
Type recognition: Convert all Nsnull types to @ ""
+ (ID) changetype: (ID) MYOBJ
{
if ([MyObj Iskindofclass:[nsdictionary class]])
{
return [self nulldic:myobj];
}
else if ([MyObj Iskindofclass:[nsarray class]])
{
return [self nullarr:myobj];
}
else if ([MyObj iskindofclass:[nsstring class]])
{
return [self stringtostring:myobj];
}
else if ([MyObj Iskindofclass:[nsnull class]])
{
return [self nulltostring];
}
Else
{
return MYOBJ;
}
}
2 How to use (green is the code that is actually used)
(1) Original JSON string: The JSON string returned by the backend contains the type Nsstring,nsarray,nsdictionary,nsnull type.
{"Status": 1, "service_name": null, "service_id": null, "Img_url": "http:\/\/api.jgfw.me\/assets\/uploads\/files\/", " Price ": null," num ": 3," Service_info ": {" Service_type ": null," Service_time ": null," Service_detail ": null," Customer_ Name ": null," Customer_phone ": null," customer_address ":" "," New_jishi ":" "," Old_jishi ": null," LAT ": null," LON ": null}, "Order_info": {"order_no": "E15031267469289848688", "pay_time": null, "Order_time": null, "Price": 0, "order_state": NULL}}
(2) Using Sbjson Library: JSON string conversion to Dictionary
Nsdictionary *jsondic = [retstring jsonvalue];
(3) converted results with a custom method: Convert all Nsnull types in the dictionary to @ ""
nsdictionary *dict = [Nsdictionary changetype:jsondic];
{
"Img_url" = "http://api.jgfw.me/assets/uploads/files/";
num = 3;
"Order_info" = {
"Order_no" = E15031267469289848688;
"Order_state" = "";
"Order_time" = "";
"Pay_time" = "";
Price = 0;
};
Price = "";
"Service_id" = "";
"Service_info" = {
"Customer_address" = "";
"Customer_name" = "";
"Customer_phone" = "";
lat = "";
lon = "";
"New_jishi" = "";
"Old_jishi" = "";
"Service_detail" = "";
"Service_time" = "";
"Service_type" = "";
};
"Service_Name" = "";
status = 1;
}
iOS development resolves nsnull types in JSON strings