1

I'm having issues accessing properties of a very simple object. As a novice programmer I suspect I have a simple misunderstanding of what exactly I'm working with.

Here is the code that is executing:

    console.log(content);
    console.log(content.title);
    for (x in content){
        console.log(content[x])
    }

And here is the console response:

{"title":"test","description":"test"}
undefined
{
"
t
i
t
l
e
"
:
"
t
e
s
t
"
,
"
d
e
s
c
r
i
p
t
i
o
n
"
:
"
t
e
s
t
"

}

Any guidance would be greatly appreciated.

edit

The content object is being sent to the server via angular.js $http service as follows:

$http({
            method:'post',
            url:"/create/createContent",
            params:{content:$scope.content}
        }).

It seems the service moves the data in the JSON format.

Thanks everyone for the help! I really appreciate it.

3
  • 1
    Can you please include how you've expressed your object? Commented Aug 15, 2013 at 19:39
  • 1
    Where do you set content? Looks like it might be a string not an object. Also for...in is often more trouble that it's worth. Commented Aug 15, 2013 at 19:40
  • I define content as an empty object (content = {}) which is added do dynamically based on user input (using angular.js). I think I must accidently be converting it to a string when it is sent to the server. Commented Aug 15, 2013 at 19:53

3 Answers 3

5

This looks like content is in fact a string and you are simply iterating over the characters in the string. Try this and see if it gives you what you want:

var contentJson = JSON.parse(content);
for (var x in contentJson) {
    console.log(contentJson[x]);
}

This will parse the content string into the appropriate JavaScript object and you should be able to access its properties.

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

Comments

3

Looks to me like the problem is how you are defining content. Are you doing this?

content = '{"title":"test","description":"test"}';

or are you doing this?

content = {"title":"test","description":"test"};

Note: the first one has the entire thing wrapped in single quotes while the second has none.

1 Comment

+1 This is also a good answer if you have control over the content variable's definition. No need to JSON.parse if you are defining the variable in your own code.
2

Your object content is in JSON string form. Do this and you will get your desired results:

content = JSON.parse(content);
console.log(content);
console.log(content.title);
for (x in content){
    console.log(content[x])
}

The way you can tell you are in JSON format is because of the quotations around the key values. If you were in object format, you wouldn't see the quotes.

See the difference below:

JSON string when output to console {"key1":1,"key2":"two"}

Object when output to console {key:1,key2:"two"}

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.