0

So I have a json object

var json = {"School":{"Keywords":"HBS","SchoolName":"","SchoolUrl":"/Careers/Apply/University_recruiting/Schools/HBS.aspx"}, "School":{"Keywords":"Stanford","SchoolName":"","SchoolUrl":"/Careers/Apply/University_recruiting/Schools/Stanford.aspx"}}

And I want to loop through it to find all of the keywords in this object. I've tried $(json.School).each(function(){ console.log(this.Keywords) } but it doesn't seem to work. Any clues?

2
  • 2
    There's no such thing as a JSON object. Commented Jun 6, 2011 at 21:24
  • 2
    I don't think that's valid JSON; it has duplicate keys in an object. Commented Jun 6, 2011 at 21:24

4 Answers 4

1

Your json object is a nested dictionary array. Reference its elements with strings, like this:

$.each(json['School'], function() {...
Sign up to request clarification or add additional context in comments.

1 Comment

You should accept answers to your questions. You have asked three, and all of them are still open.
0

$.each is used with arrays. What you have is not an array. It is a simple javascript object with properties. Here's how an array would look like in javascript:

var json = [
    {
        "Keywords":"HBS",
        "SchoolName":"",
        "SchoolUrl":"/Careers/Apply/University_recruiting/Schools/HBS.aspx"
    }, 
    {
        "Keywords":"Stanford",
        "SchoolName":"",
        "SchoolUrl":"/Careers/Apply/University_recruiting/Schools/Stanford.aspx"
    }
];

Now you can loop:

$.each(json, function() {
    console.log(this.Keywords);
});

And here's a working live demo.

6 Comments

Hmm, no it wouldn't. There's no keys in an array.
@passcod, you are correct. I've updated my answer. Thanks for pointing this out.
he's talking about "School". i think that would just error out. also, $.each works on objects, it iterates through all the properties and their values. $.each({a: 1, b: 2}, function (key, value) { // first iteration key = 'a', value = 1});
right I tried this example but still didn't work... maybe the format of the json string is incorrect?
@climboid, did you see my updated example. Here's a live demo: jsfiddle.net/V3jKG
|
0

You should use for...in

for (key in json){
    if (json.hasOwnProperty(key)) {
        alert(json[key]);
    }
}

always check if the properties is of the object to avoid properties inherited from the prototype

EDIT - that is way to iterate over properties of the object, but i don't know if yours is a valid object

Comments

0

First, json is a terrible variable name. The JSON there is invalid. The first { should be a [, and the last } should be an ]. You should probably just ditch that redundant "School" property as well.

Fixing that:

var schoolArray = [
    {"Keywords":"HBS","SchoolName":"","SchoolUrl":"/Careers/Apply/University_recruiting/Schools/HBS.aspx"},
    {"Keywords":"Stanford","SchoolName":"","SchoolUrl":"/Careers/Apply/University_recruiting/Schools/Stanford.aspx"}];

$.each(schoolArray, function(key, value)
{
    var keywords = value.Keywords;
    var schoolName = value.SchoolName;
});

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.