Search code examples
jqueryarraysjsonvimeocustom-fields

How to pull a section of a JSON array?


Hey guys I really need some help here.

My JSON feed gives me this as a key/value pair:

"custom_fields":{"zn_meta_elements":["a:8:{s:7:\"sp_link\";a:2:{s:3:\"url\";s:0:\"\";s:6:\"target\";s:6:\"_blank\";}s:6:\"sp_col\";s:0:\"\";s:14:\"sp_show_social\";s:3:\"yes\";s:10:\"port_media\";a:1:{i:0;a:3:{s:20:\"dynamic_element_type\";s:10:\"port_media\";s:21:\"port_media_image_comb\";a:3:{s:5:\"image\";s:0:\"\";s:3:\"alt\";s:0:\"\";s:5:\"title\";s:0:\"\";}s:21:\"port_media_video_comb\";s:26:\"http:\/\/vimeo.com\/112591203\";}}s:15:\"page_title_show\";s:3:\"yes\";s:10:\"page_title\";s:0:\"\";s:13:\"page_subtitle\";s:0:\"\";s:20:\"zn_disable_subheader\";s:2:\"no\";}"]}

The variable I want to get a value from is the port_media_video_comb So I want to pull the http:\/\/vimeo.com\/112591203\ part and turn it into a usable url.

The code I'm currently using to pull info looks like this:

jQuery.each(data.posts, function(i, item) {
   html += '<li>';
   html += '<div class="entry"><img src="' + data.posts[i].thumbnail + '" width="50px" height="50px" style="float:left; padding-right: 5px;"><a href="' + data.posts[i].url +'" target="_blank">' + data.posts[i].title + '</a><br /><p>' + data.posts[i].date + '</div>' ;
   html += '</li>';
});

Would I do like, data.posts[i].custom_fields.port_media_video_comb but then how to I make sure it works as a url? I'm going to use it in a <iframe> tag.

Any help is appreciated!


EDIT:

Okay so I used this site: http://json.parser.online.fr/ To help me figure out kind of the element I need to grab.

I found that I could get to the string like this: data.posts[i].custom_fields.zn_meta_elements and that returns: "a:8:{s:7:\"sp_link\";a:2:{s:3:\"url\";s:0:\"\";s:6:\"target\";s:6:\"_blank\";}s:6:\"sp_col\";s:0:\"\";s:14:\"sp_show_social\";s:3:\"yes\";s:10:\"port_media\";a:1:{i:0;a:3:{s:20:\"dynamic_element_type\";s:10:\"port_media\";s:21:\"port_media_image_comb\";a:3:{s:5:\"image\";s:0:\"\";s:3:\"alt\";s:0:\"\";s:5:\"title\";s:0:\"\";}s:21:\"port_media_video_comb\";s:26:\"http:\/\/vimeo.com\/112591203\";}}s:15:\"page_title_show\";s:3:\"yes\";s:10:\"page_title\";s:0:\"\";s:13:\"page_subtitle\";s:0:\"\";s:20:\"zn_disable_subheader\";s:2:\"no\";}" As a string.

How can I grab just the http://... part to the semi-colon and turn that into a url-friendly.. url?


Solution

  • What you need is just to get a url from a very long string so you can use regular expression to achieve this. Here is the code:

    var s = "a:8:{s:7:\"sp_link\";a:2:{s:3:\"url\";s:0:\"\";s:6:\"target\";s:6:\"_blank\";}s:6:\"sp_col\";s:0:\"\";s:14:\"sp_show_social\";s:3:\"yes\";s:10:\"port_media\";a:1:{i:0;a:3:{s:20:\"dynamic_element_type\";s:10:\"port_media\";s:21:\"port_media_image_comb\";a:3:{s:5:\"image\";s:0:\"\";s:3:\"alt\";s:0:\"\";s:5:\"title\";s:0:\"\";}s:21:\"port_media_video_comb\";s:26:\"http:\/\/vimeo.com\/112591203\";}}s:15:\"page_title_show\";s:3:\"yes\";s:10:\"page_title\";s:0:\"\";s:13:\"page_subtitle\";s:0:\"\";s:20:\"zn_disable_subheader\";s:2:\"no\";}";
    var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
    var regex = new RegExp(expression);
    var url = s.match(regex)[0]; //"http://vimeo.com/112591203"
    

    JSFiddle

    You may reference this stackoverflow article about what regular expression to use to find a url. What is a good regular expression to match a URL?