在 NSUserDefaults 中存储 swiftyJSON 返回的数据时出现问题...相同的数据源
Having issues storing swiftyJSON returned data in NSUserDefaults...same data source
很抱歉 post...大量数据。
我在将 JSON 存储到 NSUserDefaults 时遇到问题。我已经构建了后端 PHP 服务,它在两个页面上都返回 JSON。我不确定为什么一个 JSON 可以存储而另一个不能存储....
提前致谢
一般这里是 PHP CODE 其中 returns 数据
header('Content-Type: application/json; charset=utf-8');
echo json_encode($rows,JSON_PRETTY_PRINT);
socialmedia.php RETURNS:
{
"socialsites": [
{
"type": "Government",
"entries": [
{
"id": "1",
"name": "Pete",
"address": "https:\/\/website.com",
"medium": "WWW",
"section": "Government",
"zip": "02055",
"active": "Y"
}
]
},
{
"type": "School",
"entries": [
{
"id": "2",
"name": "SITE2",
"address": "https:\/\/twitter.com\/test",
"medium": "TWITTER",
"section": "School",
"zip": "02055",
"active": "Y"
}
]
},
{
"type": "Organizations",
"entries": [
{
"id": "3",
"name": "This Week Inserser",
"address": "https:\/\/www.resr.com",
"medium": "WWW",
"section": "Organizations",
"zip": "02055",
"active": "Y"
}
]
}
]
}
我可以使用
将它存储到 NSUSERDefault
var swiftyJsonVar: JSON = ""
var userdefaults = UserDefaults.standard
func gettheData(){
if let test = self.userdefaults.object(forKey: "socialtableJSON"){
print("beginkey")
print(test)
print("endkey")
}
let whichzip = userdefaults.object(forKey: "workingZip")
Alamofire.request(openEVTjson,method:.get,parameters: ["zip_code":whichzip!]).validate().responseJSON{
responseData in
switch responseData.result{
case .success(let jJson):
self.keys.removeAll()
self.post.removeAll()
self.swiftyJsonVar = JSON(jJson)
self.userdefaults.set(jJson, forKey: "socialtableJSON")
一切都很好......并且按预期工作....
eventsJSON2.php RETURNS
{
"events": [
{
"category": "SCHOOLS"
},
{
"category": "RELIGOUS ORGANIZATIONS"
},
{
"category": "NON-RELIGOUS ORGANIZATIONS",
"events2": [
{
"news_id": "12",
"news_id_parent": "12",
"series_id": "5",
"news_style": "Single-Day",
"news_finalized": "0",
"news_owner": "2",
"news_title": "TEST FOR Change JSON THIS IS A VERY LONG TITLE TO MAKE",
"news_contact": "Mr Contact",
"news_listdate": "1473944001",
"news_start_date": "1477530600",
"news_expiration": "1477537200",
"news_story": "This is the story text which will be the story text<br \/>This is the story text which will be the story text<br \/>This is the story text which will be the story text<br \/>This is the story text which will be the story text",
"news_image1": null,
"news_image2": null,
"news_ilue": "Y",
"news_priority": "N",
"news_location": "7 Main Street",
"news_location_zip": "02052",
"news_time": "",
"news_category": "3",
"news_admission": "50",
"news_needticketing": "N",
"news_tickets": "10",
"news_tickets_pur": null,
"news_tickets_ser": "Y",
"news_phone": "555-555-5555",
"news_email": "no@no.com",
"news_web": "http:\/\/www.cnn.com",
"news_sponsor": "Me Sponsor",
"news_story_released": "Y",
"news_organization": "1",
"news_basezip": "02052",
"news_surround": "N",
"news_attach": null,
"news_paid": "NP",
"news_weeks": "1",
"news_rate": null,
"txn_id": null,
"news_pay_auth_date": null,
"news_pay_date": null,
"org_id": "1",
"org_name": "New N' Towne",
"base_zip": "02052",
"description": "Organization for New People",
"cat_id": "3"
}
]
},
{
"category": "GOVERNMENT"
},
{
"category": "RETAIL"
},
{
"category": "YARD SALES"
},
{
"category": "OPEN HOUSES"
}
]
}
我去存储用
var swiftyJsonVar: JSON = ""
var userdefaults = UserDefaults.standard
Alamofire.request(openEVTjson,method:.get,parameters: ["zip_code":whichzip]).validate().responseJSON{
responseData in
switch responseData.result{
case .success(let jJson):
self.swiftyJsonVar = JSON(jJson)
self.keys.removeAll()
//let test = self.swiftyJsonVar.rawString()
//print(test)
self.userdefaults.set(jJson, forKey: "lastEventsJSON")
self.userdefaults.set(Date().timeIntervalSince1970, forKey: "lastEventsLoad")
最后我得到
2016-10-26 08:55:31.813 JSONTesting[79483:4081659] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object {
events = (
{
category = SCHOOLS;
},
{
category = "RELIGOUS ORGANIZATIONS";
},
{
category = "NON-RELIGOUS ORGANIZATIONS";
events2 = (
{
"base_zip" = 02052;
"cat_id" = 3;
description = "Organization for New People";
"news_admission" = 50;
"news_attach" = "<null>";
"news_basezip" = 02052;
"news_category" = 3;
"news_contact" = "Mr Contact";
"news_email" = "no@no.com";
"news_expiration" = 1477537200;
"news_finalized" = 0;
"news_id" = 12;
"news_id_parent" = 12;
"news_ilue" = Y;
"news_image1" = "<null>";
"news_image2" = "<null>";
"news_listdate" = 1473944001;
"news_location" = "7 Main Street";
"news_location_zip" = 02052;
"news_needticketing" = N;
"news_organization" = 1;
"news_owner" = 2;
"news_paid" = NP;
"news_pay_auth_date" = "<null>";
"news_pay_date" = "<null>";
"news_phone" = "555-555-5555";
"news_priority" = N;
"news_rate" = "<null>";
"news_sponsor" = "Me Sponsor";
"news_start_date" = 1477530600;
"news_story" = "This is the story text which will be the story text<br />This is the story text which will be the story text<br />This is the story text which will be the story text<br />This is the story text which will be the story text";
"news_story_released" = Y;
"news_style" = "Single-Day";
"news_surround" = N;
"news_tickets" = 10;
"news_tickets_pur" = "<null>";
"news_tickets_ser" = Y;
"news_time" = "";
"news_title" = "TEST FOR Change JSON THIS IS A VERY LONG TITLE TO MAKE";
"news_web" = "http://www.cnn.com";
"news_weeks" = 1;
"org_id" = 1;
"org_name" = "New N' Towne";
"series_id" = 5;
"txn_id" = "<null>";
}
);
},
{
category = GOVERNMENT;
},
{
category = RETAIL;
},
{
category = "YARD SALES";
},
{
category = "OPEN HOUSES";
}
);
} for key lastEventsJSON'
您需要 trim 通过删除 NSNull 值或用默认值替换它们来 JSON 。
很抱歉 post...大量数据。 我在将 JSON 存储到 NSUserDefaults 时遇到问题。我已经构建了后端 PHP 服务,它在两个页面上都返回 JSON。我不确定为什么一个 JSON 可以存储而另一个不能存储....
提前致谢
一般这里是 PHP CODE 其中 returns 数据
header('Content-Type: application/json; charset=utf-8');
echo json_encode($rows,JSON_PRETTY_PRINT);
socialmedia.php RETURNS:
{
"socialsites": [
{
"type": "Government",
"entries": [
{
"id": "1",
"name": "Pete",
"address": "https:\/\/website.com",
"medium": "WWW",
"section": "Government",
"zip": "02055",
"active": "Y"
}
]
},
{
"type": "School",
"entries": [
{
"id": "2",
"name": "SITE2",
"address": "https:\/\/twitter.com\/test",
"medium": "TWITTER",
"section": "School",
"zip": "02055",
"active": "Y"
}
]
},
{
"type": "Organizations",
"entries": [
{
"id": "3",
"name": "This Week Inserser",
"address": "https:\/\/www.resr.com",
"medium": "WWW",
"section": "Organizations",
"zip": "02055",
"active": "Y"
}
]
}
]
}
我可以使用
将它存储到 NSUSERDefaultvar swiftyJsonVar: JSON = ""
var userdefaults = UserDefaults.standard
func gettheData(){
if let test = self.userdefaults.object(forKey: "socialtableJSON"){
print("beginkey")
print(test)
print("endkey")
}
let whichzip = userdefaults.object(forKey: "workingZip")
Alamofire.request(openEVTjson,method:.get,parameters: ["zip_code":whichzip!]).validate().responseJSON{
responseData in
switch responseData.result{
case .success(let jJson):
self.keys.removeAll()
self.post.removeAll()
self.swiftyJsonVar = JSON(jJson)
self.userdefaults.set(jJson, forKey: "socialtableJSON")
一切都很好......并且按预期工作....
eventsJSON2.php RETURNS
{
"events": [
{
"category": "SCHOOLS"
},
{
"category": "RELIGOUS ORGANIZATIONS"
},
{
"category": "NON-RELIGOUS ORGANIZATIONS",
"events2": [
{
"news_id": "12",
"news_id_parent": "12",
"series_id": "5",
"news_style": "Single-Day",
"news_finalized": "0",
"news_owner": "2",
"news_title": "TEST FOR Change JSON THIS IS A VERY LONG TITLE TO MAKE",
"news_contact": "Mr Contact",
"news_listdate": "1473944001",
"news_start_date": "1477530600",
"news_expiration": "1477537200",
"news_story": "This is the story text which will be the story text<br \/>This is the story text which will be the story text<br \/>This is the story text which will be the story text<br \/>This is the story text which will be the story text",
"news_image1": null,
"news_image2": null,
"news_ilue": "Y",
"news_priority": "N",
"news_location": "7 Main Street",
"news_location_zip": "02052",
"news_time": "",
"news_category": "3",
"news_admission": "50",
"news_needticketing": "N",
"news_tickets": "10",
"news_tickets_pur": null,
"news_tickets_ser": "Y",
"news_phone": "555-555-5555",
"news_email": "no@no.com",
"news_web": "http:\/\/www.cnn.com",
"news_sponsor": "Me Sponsor",
"news_story_released": "Y",
"news_organization": "1",
"news_basezip": "02052",
"news_surround": "N",
"news_attach": null,
"news_paid": "NP",
"news_weeks": "1",
"news_rate": null,
"txn_id": null,
"news_pay_auth_date": null,
"news_pay_date": null,
"org_id": "1",
"org_name": "New N' Towne",
"base_zip": "02052",
"description": "Organization for New People",
"cat_id": "3"
}
]
},
{
"category": "GOVERNMENT"
},
{
"category": "RETAIL"
},
{
"category": "YARD SALES"
},
{
"category": "OPEN HOUSES"
}
]
}
我去存储用
var swiftyJsonVar: JSON = ""
var userdefaults = UserDefaults.standard
Alamofire.request(openEVTjson,method:.get,parameters: ["zip_code":whichzip]).validate().responseJSON{
responseData in
switch responseData.result{
case .success(let jJson):
self.swiftyJsonVar = JSON(jJson)
self.keys.removeAll()
//let test = self.swiftyJsonVar.rawString()
//print(test)
self.userdefaults.set(jJson, forKey: "lastEventsJSON")
self.userdefaults.set(Date().timeIntervalSince1970, forKey: "lastEventsLoad")
最后我得到
2016-10-26 08:55:31.813 JSONTesting[79483:4081659] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object {
events = (
{
category = SCHOOLS;
},
{
category = "RELIGOUS ORGANIZATIONS";
},
{
category = "NON-RELIGOUS ORGANIZATIONS";
events2 = (
{
"base_zip" = 02052;
"cat_id" = 3;
description = "Organization for New People";
"news_admission" = 50;
"news_attach" = "<null>";
"news_basezip" = 02052;
"news_category" = 3;
"news_contact" = "Mr Contact";
"news_email" = "no@no.com";
"news_expiration" = 1477537200;
"news_finalized" = 0;
"news_id" = 12;
"news_id_parent" = 12;
"news_ilue" = Y;
"news_image1" = "<null>";
"news_image2" = "<null>";
"news_listdate" = 1473944001;
"news_location" = "7 Main Street";
"news_location_zip" = 02052;
"news_needticketing" = N;
"news_organization" = 1;
"news_owner" = 2;
"news_paid" = NP;
"news_pay_auth_date" = "<null>";
"news_pay_date" = "<null>";
"news_phone" = "555-555-5555";
"news_priority" = N;
"news_rate" = "<null>";
"news_sponsor" = "Me Sponsor";
"news_start_date" = 1477530600;
"news_story" = "This is the story text which will be the story text<br />This is the story text which will be the story text<br />This is the story text which will be the story text<br />This is the story text which will be the story text";
"news_story_released" = Y;
"news_style" = "Single-Day";
"news_surround" = N;
"news_tickets" = 10;
"news_tickets_pur" = "<null>";
"news_tickets_ser" = Y;
"news_time" = "";
"news_title" = "TEST FOR Change JSON THIS IS A VERY LONG TITLE TO MAKE";
"news_web" = "http://www.cnn.com";
"news_weeks" = 1;
"org_id" = 1;
"org_name" = "New N' Towne";
"series_id" = 5;
"txn_id" = "<null>";
}
);
},
{
category = GOVERNMENT;
},
{
category = RETAIL;
},
{
category = "YARD SALES";
},
{
category = "OPEN HOUSES";
}
);
} for key lastEventsJSON'
您需要 trim 通过删除 NSNull 值或用默认值替换它们来 JSON 。