Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 解析序列化JSON_Jquery_Json_Wordpress_Mobile_Serialization - Fatal编程技术网

Jquery 解析序列化JSON

Jquery 解析序列化JSON,jquery,json,wordpress,mobile,serialization,Jquery,Json,Wordpress,Mobile,Serialization,我正在使用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"

我正在使用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:"";}}"
],
大多数JSON数据都可以使用“$.each”轻松访问。但自定义_字段保存的数据似乎是在数组中序列化的(有“Repeater fields”,前缀为:n,其中a表示数组,n表示该数组的出现次数(我认为)

因此,我正在寻找一种简单的方法来反序列化数据,以便能够访问每个数组和数组的每个元素

有没有人做过这件事,并举了一个例子

我可以使用(例如)访问特定的自定义字段

但是找不到一种简单的jquery方法来解包序列化数据

代码使用Ajax在jquery移动应用程序中下载JSON。

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];

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:\"\";}}"],

谢谢。我试着让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:\"\";}}"],