1

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?

ButlerWeb
  • 23
  • 5
  • This is not JSON. It is a different form of serialization (PHP perhaps?). You need to find appropriate deserializer and deserialize into a data structure you can read the data you want from. – Mike Brant Dec 02 '14 at 05:03
  • Why don't you just deserialize this string so that you can simply access the properties you need: custom_fields = JSON.parse(custom_fields); That's the whole point of JSON: no need to deal with strings, this is an object!! – frenchie Dec 02 '14 at 05:10

1 Answers1

-1

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?

Community
  • 1
  • 1
user2466202
  • 1,205
  • 10
  • 8
  • The OP should focus on deserialization not regex manipulation. – Mike Brant Dec 02 '14 at 05:05
  • Mike, "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\";}" is not a json string so how can you deserialize it? – user2466202 Dec 02 '14 at 05:23
  • It appears to be a PHP serialization, so one would need to deserialize it using PHP, deserialize using a javascript implementation of PHP desrialization such as http://phpjs.org/functions/unserialize/, or fix the API itself to actually return JSON (if this is under his control). – Mike Brant Dec 02 '14 at 14:26
  • I'm using a JSON API plugin for wordpress that returns this information. I'll look into deserialization. Never dealt with this before so thank you :) Also the regexp stuff is so confusing to me but the code above seems to work thus far, so thank you! – ButlerWeb Dec 03 '14 at 17:22