1
  1. On the controller, my code is like:

    format.json {render :json=>{
      :entities => @entities,
      :entity_category_counters => @entity_category_counters
    }}
    
  2. On the Browser side, my code is like:

    $.getJSON('/entities.json',function(data){
    //data.entities
    //data[0].entities
    //data['entities']
    });
    

    No matter which way I use to access the return JSON object's data, I cannot succeed.

  3. The returned JSON is like:

    {"entities":[{"entity":{"created_at":"2011-08-01T18:14:32Z","description":"xxx"}},{"entity":{"created_at":"2011-08-01T18:16:02Z","description":"xxx"}}],"entity_category_counters":[{"entity_category_counter":{"comment_category_id":1,"counter":4,"entity_id":1,"id":1,"important_tag":false}},{"entity_category_counter":{"comment_category_id":2,"counter":0,"entity_id":1,"id":2,"important_tag":false}},{"entity_category_counter":{"comment_category_id":3,"counter":0,"entity_id":1,"id":3,"important_tag":false}},{"entity_category_counter":{"comment_category_id":1,"counter":3,"entity_id":2,"id":4,"important_tag":false}},{"entity_category_counter":{"comment_category_id":2,"counter":0,"entity_id":2,"id":5,"important_tag":false}},{"entity_category_counter":{"comment_category_id":3,"counter":1,"entity_id":2,"id":6,"important_tag":false}}]}

  4. If I display the data directly with Alert like the following:

    $.getJSON('/entities.json',function(data){
    //data.entities
    //data[0].entities
    //data['entities']
    alert(data);
    
    });
    

the result in the message box is like:

    [object Object],[object Object]

So can anybody tell me how to parse and access data in the returned JSON object that contains two objects? Thanks in advance!

1 Answer 1

3

JSON stands for JavaScript Object Notation, so you're working in pure javascript at this point. You could do something like this:

data.entities[0].entity.created_at // 2011-08-01T18:14:32Z

I think what's confusing about your about is that entities is an array, thus you have to access an index of that array to get at the entity in it. Then you also have the entity categories.

I think you'd find several things helpful:

  • JSONLint (http://jsonlint.com/) paste in your json code and have it formatted so you can see whether objects are directly accessible or in an array in which case you have to iterate over it or access a specific index
  • Your browser console. In google chrome, you can right click in the page, select "Inspect Element", then click the Console tab. From there you can execute javascript in the context of the page you're on. I pasted in your json output into the console to play with it: test = {"entities...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.