Results 1 to 6 of 6
  1.    #1  
    Hey guys, I've successfully worked with JSON data in the past, but have stumbled on a new issue while working with a new API.

    In the chain of data, JSON format, some of the names are numbers:
    Code:
    {
      "content": {
        "users": {
          "0": {
            "user": [
              //more stuff in here that I need to get to
            ]
          }
        }
      }
    }
    And here is my code for parsing it:

    Code:
            var request = new Ajax.Request(url,
            {
                method: 'GET',
                onSuccess: function(transport) {
                    var list = transport.responseJSON.content.users.0.user[0];
                    Mojo.Log.error("List: " + list);
                },
                onFailure: function() {
                    Mojo.Log.error("Data Call Failed");
                }
            });
    When building the variable "list", I get errors because of the ".0." section. I can't do anything to take the "0" out of the JSON response, but need to access data deeper than the zero.

    Any ideas on how to address the names that are numbers?

    Thanks!

    David
  2. #2  
    Quote Originally Posted by dbarkman View Post
    Hey guys, I've successfully worked with JSON data in the past, but have stumbled on a new issue while working with a new API.

    In the chain of data, JSON format, some of the names are numbers:
    Code:
    {
      "content": {
        "users": {
          "0": {
            "user": [
              //more stuff in here that I need to get to
            ]
          }
        }
      }
    }
    And here is my code for parsing it:

    Code:
            var request = new Ajax.Request(url,
            {
                method: 'GET',
                onSuccess: function(transport) {
                    var list = transport.responseJSON.content.users.0.user[0];
                    Mojo.Log.error("List: " + list);
                },
                onFailure: function() {
                    Mojo.Log.error("Data Call Failed");
                }
            });
    When building the variable "list", I get errors because of the ".0." section. I can't do anything to take the "0" out of the JSON response, but need to access data deeper than the zero.

    Any ideas on how to address the names that are numbers?

    Thanks!

    David
    Try this:


    Code:
            var request = new Ajax.Request(url,
            {
                method: 'GET',
                onSuccess: function(transport) {
                    var list = transport.responseJSON.content.users["0"].user[0];
                    Mojo.Log.error("List: " + list);
                },
                onFailure: function() {
                    Mojo.Log.error("Data Call Failed");
                }
            });
    That syntax is also handy if there is a name contains other characters, like spaces or punctuation. Any legal string can be used as a name.
  3.    #3  
    Thanks for the reply, but it didn't seem to work. The zeros have to be called out just as any other part of the chain, between two periods. The zero is a level itself and I can't seem to get past it until I figure out how to refer to it.

    Thanks

    David
  4. #4  
    Odd. It should have worked. For example this:

    Code:
    foo.bar.smeg
    Is the equivalent of this:

    Code:
    foo["bar"]["smeg"]
  5.    #5  
    It did work! In the next step of the chain where I had "user[0]", I should have put "user[1]". Once I did, it worked great! After starring at the JSON data for a few minutes, I realized that the data I wanted was in the second item of the array or "[1]".

    Thank you very much!

    David
  6. #6  
    np, glad I could help.

Posting Permissions