0

Receiving JSON response like this

responses = {
            "http://www.example.com/firts": {
                error: [object Object],
                response: [object Object],
                body: [object Object]
            },
            "http://www.example.com/second": {
                error: [object Object],
                response: [object Object],
                body: [object Object]
            },
            "http://www.example.com/third": {
                error: [object Object],
                response: [object Object],
                body: [object Object]
            }
        }

var urls = ["http://www.example.com/firts", "http://www.example.com/second", "http://www.example.com/third"];

It works fine in for loop like this:

for(url in responses) {
        var response = responses[url];
        console.log('Got the response '+response.response.statusCode);
}

But I am not able to access it outside for loop. Tried:

var response = responses[urls[0]];
console.log('Got the response '+response.response.statusCode);

and

var response = responses["http://www.example.com/firts"];
console.log('Got the response '+response.response.statusCode);

and

var response = responses[0][urls[0]];
    console.log('Got the response '+response.response.statusCode);

But nothing worked for me.

2
  • Are there any additional spaces or characters you're not seeing? Commented Mar 10, 2014 at 21:42
  • Did you try (lint)[jsonlint.com] (trick question, not valid json) Commented Mar 10, 2014 at 21:43

1 Answer 1

3

Your JSON isn't JSON. It is a set of JavaScript object literals, and when it hits [object Object], it errors because you can't have someIdentifier someOtherIdentifier in an array literal.

Values in arrays have to be separated by commas, but it looks like you intended to have object literals there with some specific values. When you create the JavaScript, you need to express those as proper object literals instead of simply casting the objects to strings.

Sign up to request clarification or add additional context in comments.

1 Comment

I am actually using https://gist.github.com/natos/2001487 program. Not sure how would I implement the proper object literals?

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.