我正在使用Wordpress JSON API插件生成JSON,以便在移动应用中使用。
数据来自Wordpress中自定义帖子类型的自定义字段(通过名为WCK的插件创建)
wordpress自定义字段的JSON摘录如下所示
custom_fields: {
sitedetails: [
"a:1:{i:0;a:5:{s:14:"site-reference";s:4:"A428";s:9:"site-name";s:11:"Main street";s:16:"site-description";s:19:"A great development";s:10:"site-image";s:2:"70";s:24:"site-data-version-number";s:3:"1.0";}}"
],
plotdetails: [
"a:1:{i:0;a:7:{s:14:"plot-reference";s:5:"A4282";s:12:"plot-address";s:42:"9 Highers Way Threwsbury Thropshire XY11AA";s:10:"plot-image";s:2:"70";s:12:"map-latitude";s:9:"22.111122";s:13:"map-longitude";s:9:"-9.334455";s:13:"plot-postcode";s:7:"XY11AA";s:24:"plot-data-version-number";s:3:"1.0";}}"
],
ownersdetails: [
"a:0:{}"
],
housingassociation: [
"a:0:{}"
],
movingin: [
"a:1:{i:0;a:1:{s:16:"moving-in-advice";s:91:"Make sure you know where the various stop-valves and main electrical switches are located. ";}}"
],
repairstoyourhome: [
"a:1:{i:0;a:5:{s:14:"repairs-advice";s:9:"Take care";s:12:"phone-number";s:13:"0123 45 67 89";s:7:"website";s:21:"http://www.google.com";s:5:"email";s:16:"google@gmail.com";s:24:"repairs-additional-notes";s:11:"Please call";}}"
],
certificates: [
"a:5:{i:0;a:3:{s:16:"certificate-name";s:34:"NICEIC Electrical Test Certificate";s:17:"certificate-notes";s:24:"Refer to your paper copy";s:20:"certificate-document";s:0:"";}i:1;a:3:{s:16:"certificate-name";s:20:"Gas Safe Certificate";s:17:"certificate-notes";s:24:"Refer to your paper copy";s:20:"certificate-document";s:0:"";}i:2;a:3:{s:16:"certificate-name";s:23:"Smoke Alarm Certificate";s:17:"certificate-notes";s:19:"Refer to paper copy";s:20:"certificate-document";s:0:"";}i:3;a:3:{s:16:"certificate-name";s:30:"Energy Performance Certificate";s:17:"certificate-notes";s:19:"Refer to paper copy";s:20:"certificate-document";s:0:"";}i:4;a:3:{s:16:"certificate-name";s:29:"Sustainable Homes Certificate";s:17:"certificate-notes";s:19:"Refer to paper copy";s:20:"certificate-document";s:0:"";}}"
],
使用“$ .each”可以轻松访问大多数JSON数据。但是custom_fields保存的数据似乎是在数组中序列化的(有“Repeater字段”,前缀为:n,其中a表示数组,n表示该数组的出现次数(我认为)。
所以,我正在寻找一种简单的反序列化数据的方法,这样我就可以访问每个数组和数组的每个元素。
有没有人这样做并有一个例子?
我可以使用(例如)
访问特定的自定义字段data.post.custom_fields.sitedetails
data.post.custom_fields.plotdetails
等
但是找不到简单的jquery方法来解压缩序列化数据。
该代码使用Ajax在jquery移动应用程序中下载JSON。
答案 0 :(得分:0)
var
obj = JSON.parse( yourJsonVariable );
但你的JSON正处于完整的畸形状态......
以逗号结束... 和证书之类的对象缺少转义字符:
certificates: [
"a:5:{i:0;a:3:{s:16:\"certificate-name\";s:34:\"NICEIC Electrical Test Certificate\";s:17:\"certificate-notes\";s:24:\"Refer to your paper copy\";s:20:\"certificate-document\";s:0:\"\";}i:1;a:3:{s:16:\"certificate-name\";s:20:\"Gas Safe Certificate\";s:17:\"certificate-notes\";s:24:\"Refer to your paper copy\";s:20:\"certificate-document\";s:0:\"\";}i:2;a:3:{s:16:\"certificate-name\";s:23:\"Smoke Alarm Certificate\";s:17:\"certificate-notes\";s:19:\"Refer to paper copy\";s:20:\"certificate-document\";s:0:\"\";}i:3;a:3:{s:16:\"certificate-name\";s:30:\"Energy Performance Certificate\";s:17:\"certificate-notes\";s:19:\"Refer to paper copy\";s:20:\"certificate-document\";s:0:\"\";}i:4;a:3:{s:16:\"certificate-name\";s:29:\"Sustainable Homes Certificate\";s:17:\"certificate-notes\";s:19:\"Refer to paper copy\";s:20:\"certificate-document\";s:0:\"\";}}"
],
在此之后,你应该像你想要的那样解析它:
obj = JSON.parse( youJsonVariable );
obj.custom_fields.certificates[0];
答案 1 :(得分:0)
在jquery中收到的实际JSON: -
"custom_fields":{"sitedetails":["a:1:{i:0;a:5:{s:14:\"site-reference\";s:4:\"A428\";s:9:\"site-name\";s:11:\"Main street\";s:16:\"site-description\";s:19:\"A great development\";s:10:\"site-image\";s:2:\"70\";s:24:\"site-data-version-number\";s:3:\"1.0\";}}"],"plotdetails":["a:1:{i:0;a:7:{s:14:\"plot-reference\";s:5:\"A4282\";s:12:\"plot-address\";s:42:\"9 Highers Way\nThrewsbury\nThropshire\nXY11AA\";s:10:\"plot-image\";s:2:\"70\";s:12:\"map-latitude\";s:9:\"22.112233\";s:13:\"map-longitude\";s:9:\"-9.121212\";s:13:\"plot-postcode\";s:7:\"XY1 1AA\";s:24:\"plot-data-version-number\";s:3:\"1.0\";}}"],"ownersdetails":["a:0:{}"],"housingassociation":["a:0:{}"],"movingin":["a:1:{i:0;a:1:{s:16:\"moving-in-advice\";s:91:\"Make sure you know where the various stop-valves and main electrical switches are located.\n\";}}"],"repairstoyourhome":["a:1:{i:0;a:5:{s:14:\"repairs-advice\";s:9:\"Take care\";s:12:\"phone-number\";s:13:\"0123 45 67 89\";s:7:\"website\";s:21:\"http:\/\/www.google.com\";s:5:\"email\";s:16:\"google@gmail.com\";s:24:\"repairs-additional-notes\";s:11:\"Please call\";}}"],"certificates":["a:5:{i:0;a:3:{s:16:\"certificate-name\";s:34:\"NICEIC Electrical Test Certificate\";s:17:\"certificate-notes\";s:24:\"Refer to your paper copy\";s:20:\"certificate-document\";s:0:\"\";}i:1;a:3:{s:16:\"certificate-name\";s:20:\"Gas Safe Certificate\";s:17:\"certificate-notes\";s:24:\"Refer to your paper copy\";s:20:\"certificate-document\";s:0:\"\";}i:2;a:3:{s:16:\"certificate-name\";s:23:\"Smoke Alarm Certificate\";s:17:\"certificate-notes\";s:19:\"Refer to paper copy\";s:20:\"certificate-document\";s:0:\"\";}i:3;a:3:{s:16:\"certificate-name\";s:30:\"Energy Performance Certificate\";s:17:\"certificate-notes\";s:19:\"Refer to paper copy\";s:20:\"certificate-document\";s:0:\"\";}i:4;a:3:{s:16:\"certificate-name\";s:29:\"Sustainable Homes Certificate\";s:17:\"certificate-notes\";s:19:\"Refer to paper copy\";s:20:\"certificate-document\";s:0:\"\";}}"],