Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to search within an array of a json file?

    I have the attached json data that I have fetched with an API from commafeed.com. I am using it to curate my webpage here: http://faculty-fight...jfdeoijn/tiles/. I would like to also include an image of each news post. Which is within the "content" array. However, within the content array is also the actual content. How can I only extract the image URL so that I can .append it to my <div></div> as a background-image?

    Over at stackoverflow somebody suggested:

    Code:
    jQuery(data.entries[0].content.match(/<[^>]+>/)[0]).appendTo('.para');
    Which is a great start, but I only need the Http://www.url.de/someimage.jpg. I tried Regex (http://gskinner.com/RegExr/), but I either get an unknown token error or an Uncaught TypeError: Cannot read property '0' of undefined error. I am starting to figure the problem is over at http://faculty-fight.de/jquery/ where I fetch my data.

    What do you guys think?


    My jquery looks like this:

    Code:
    $("#puttiles").ready(function (event) { $.ajax({ type: 'GET', url: "source", dataType: 'json', success: function (jso) { $.each(jso.entries, function (entryIndex, entry) { $('#puttiles').append("<div class='element'>" + this.content.match(/<[^>]+>/)[0] + "<div class='story-tile-byline'><div class='tile-title'> " + this.title + "</div><div class='source;>Gizmodo</div></div></div>"); }); } }); });
    My json like this:

    Code:
    {
      "name": "Aktuell - FAZ.NET",
      "message": null,
      "errorCount": 0,
      "feedLink": "http://www.faz.net/aktuell/",
      "timestamp": 1375222681971,
      "hasMore": true,
      "entries": [
        {
          "id": "2011990942",
          "guid": "http://www.faz.net/-hoz-7bwlz",
          "title": "Keine „Unterstützung des Feindes“ durch Bradley Manning",
          "content": "<img width=\"190\" height=\"107\" title=\"Eine Unterst&uuml;tzerin von Manning vor dem Milit&auml;rgericht in Fort Meade\" alt=\"Eine Unterst&uuml;tzerin von Manning vor dem Milit&auml;rgericht in Fort Meade\" src=\"http://media1.faz.net/polopoly_fs/1.2312835!/image/2672702860.jpg_gen/derivatives/article_teaser/2672702860.jpg\" /><p>Der Wikileaks-Informant Bradley Manning ist von einem Milit&auml;rgericht in 19 von 21 Anklagepunkten schuldig gesprochen worden. Freigesprochen wurde er vom Vorwurf der „Unterst&uuml;tzung des Feindes“. Damit droht ihm keine Todesstrafe.</p>",
          "rtl": false,
          "author": null,
          "enclosureUrl": null,
          "enclosureType": null,
          "date": 1375204740000,
          "insertedDate": 1375212854000,
          "feedId": "97761879",
          "feedName": "Aktuell - FAZ.NET",
          "feedUrl": "http://www.faz.net/rss/aktuell/",
          "feedLink": "http://www.faz.net/aktuell/",
          "iconUrl": "https://www.commafeed.com/rest/feed/favicon/97761879",
          "url": "http://www.faz.net/aktuell/politik/ausland/amerika/wikileaks-informant-keine-unterstuetzung-des-feindes-durch-bradley-manning-12312791.html",
          "read": true,
          "starred": false,
          "markable": true
        }}

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Code:
    jQuery(data.entries[0].content.match(/http:\/\/[\w\W]+?\.jpg/i)[0]).appendTo('.para');
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Alternative solution:
    Code:
    var imgSrc = $(data.entries[0].content).attr('src');
    alert(imgSrc);

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by glenngv View Post
    Alternative solution:
    Code:
    var imgSrc = $(data.entries[0].content).attr('src');
    alert(imgSrc);
    that's nifty, but it will ping the img's url when the HTML is parsed, and execute any embedded scripts should they appear, which is why i went RegExp...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Yeah that will download the image but the OP said he wanted the img src to be the background image. So eventually the image really needs to be downloaded.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •