【问题标题】:Parse serialized JSON解析序列化的 JSON
【发布时间】:2014-02-03 21:13:46
【问题描述】:

我正在使用 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 保存的数据似乎在数组中被序列化(有前缀为 a:n 的“中继器字段”,其中 a 表示数组,n 是该数组的出现次数(我认为)。

所以,我正在寻找一种简单的方法来反序列化数据,这样我就可以访问每个数组和数组的每个元素。

有没有人做过这个并且有例子?

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

data.post.custom_fields.sitedetails
data.post.custom_fields.plotdetails

但是找不到简单的jquery方式来解压序列化数据。

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

【问题讨论】:

    标签: jquery json wordpress mobile serialization


    【解决方案1】:

    变量

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

    【讨论】:

    • 谢谢。我试图使 JSON 可读。它实际上看起来像这样:-
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-05-17
      • 2015-07-28
      • 1970-01-01
      • 2015-09-21
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多