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 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    33
    Thanks
    18
    Thanked 0 Times in 0 Posts

    JSON & SimpleXML: no attributes

    Hello,

    I return a JSON object via ajax the following way:

    PHP Code:
    <?php
    $rss 
    simplexml_load_file('http://news.yahoo.com/rss/sports'); 

    $rssarray=array();

    foreach(
    $rss->channel->item as $item) {
        
    array_push($rssarrayjson_encode($item->title));
    }    

    echo 
    json_encode($rssarray);  
    ?>
    The content is parsed like this:
    {"0":"AP Source: J.R. Smith wins NBA sixth man"}
    {"0":"Jets trade Darrelle Revis to Buccaneers"}
    {"0":"Ginobili, Parker lead Spurs past Lakers in Game 1"}
    etc...

    I however only want to parse the actual title, not the attributes such as {"0":}, like this:
    AP Source: J.R. Smith wins NBA sixth man
    Jets trade Darrelle Revis to Buccaneers
    Ginobili, Parker lead Spurs past Lakers in Game 1
    etc...

    This sounds quite basic I however can't solve this other than thinking of some splitter function :/

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    If you know that there will always be two parts, just use str.split(":")[1] for the data.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • Users who have thanked WolfShade for this post:

    coffeecup (04-26-2013)

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    it's already JSON so
    no need for json_encode

    Code:
     
    <doctype html>
    <html>
    <head>
    <script>
    rss = <?php 
    $rss = simplexml_load_file('http://news.yahoo.com/rss/sports');  
    $rssarray=array(); 
    foreach($rss->channel->item as $item) { 
        array_push($rssarray, $item->title); 
    }     
    echo json_encode($rssarray);   
    ?> 
    </script>
    </head>
    <body>
    <script>
    for(var i=0;i<rss.length;i++)
    document.write(rss[i]["0"],"<br>")
    </script>
    </body>
    </html>

  • Users who have thanked DaveyErwin for this post:

    coffeecup (04-26-2013)

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,298
    Thanks
    10
    Thanked 584 Times in 565 Posts
    not sure if you just forgot to draw the array brackets, or if that's your response and you clumed on object together as many, whatever the case, it's not how json is.

    you can be tricky and use generics to functionally iterate over an array-like object, if that's what you have:

    Code:
    var resp={
     "0":"AP Source: J.R. Smith wins NBA sixth man",
     "1":"Jets trade Darrelle Revis to Buccaneers",
     "2":"Ginobili, Parker lead Spurs past Lakers in Game 1"
    };
    
    resp.length=Object.keys(resp).length;
    
    alert(
      [].map.call( resp, String)
    );
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    coffeecup (04-26-2013)


  •  

    Posting Permissions

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