views:

71

answers:

2

I am trying to process a json version of a rss feed but I need to access an array labeled "data.channel.item". I suspect because this name seems to conflict with the reserved word item in javascript, the script doesn't work with only this particular json file. It does work in other cases.

Is there a way to use "item" but escape it somehow? (Yes, I'm a javascript newbie)

Here is the script (jquery)

 <script>
  $(document).ready(function(){
    $.getJSON("http://example.com/jsonBuzz.php?callback=?",
        function(data){
          $.each(data.channel.item, function(i,item){
            $("#results").append('<li><h1>'+item.title+'</h1></li>');
          });
        });
  });

  </script>

The abridged version of the json:

{"@attributes":{"version":"2.0"},"channel":{"lastBuildDate":"Mon, 11 Jan 2010... ..."generator":"Blogger","item":[{"guid":"tag:blogger.com,...
+6  A: 

Putting the keys in double quotes will allow you to use a reserved word--in fact, it's required to be valid JSON (see http://json.org/). But, this isn't really the problem, as "item" isn't a reserved word in JavaScript (see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Reserved_Words as one possible reference).

You've got item:title, rather than item.title or item["title"], both of which are the correct ways to access an object's properties.

miketaylr
OK, thanks for the list of reserved words. Indeed, item is not in it. However, my code editor boldens the word so I thought there must be something special about it. I made the other change you suggested and still doesn't work. Like I said, I know this script works on other json.I'm editing my original post to fix the ":" typo.
buzzspree
Which code editor are you using? `item` is usually a member of a Collection object but that certainly doesn't make it reserved, nor should a js code editor highlight the word.
Andy E
I see this when I use Dreamweaver MX 2004.
buzzspree
A: 

For the record: the problem, it turns out, wasn't with the javascript code. Inspired by a question (Andy E) in comments, I found out how to catch javascript errors and determined that the json wasn't properly enclosed in parenthesis or end with ";".

This was a PHP issue. PHP has a useful function json_encode which converts an PHP object to json.

However, being new to json, I assumed that this was ready for consumption. Little did I know it needed to be wrapped in a few extra characters. I revised the code and it now works as planned.

Below is the PHP code others may find useful. This is a php file I retrieve through jquery $.getJSON as in my first post:

<?php
header('Content-Type: text/javascript; charset=utf8');
header('Access-Control-Allow-Origin: http://www.example.com/');
header('Access-Control-Max-Age: 3628800');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');

$file='rss.xml';
$arr = simplexml_load_file($file);//this creates an object from the xml file
$json= '('.json_encode($arr).');'; //must wrap in parens and end with semicolon
print_r($_GET['callback'].$json); //callback is prepended for json-p
?>
buzzspree