2

I have following JSON string in a global variable in Javascript

var domains = {
    "DomID00001": {
        "ID":"DomID00001",
        "Name":"Testdomein1"
    },
    "DomID00002": {
        "ID":"DomID00002",
        "Name":"Testdomein2"
    }
};

What is the best way to retrieve the values from this JSON string and be able to use them in Javascript individually?

If I just alert the domains var it says = [object Object]

3
  • 2
    That's not a JSON string, it's a JavaScript object. You access it like any other JavaScript object. Commented Dec 13, 2013 at 8:53
  • Objects cannot be alerted. So console it, like console.log(domains) Commented Dec 13, 2013 at 8:53
  • 1
    There is no JSON in your example anywhere in sight. At first I rejoiced - somebody said JSON string - but then you have an object. Usually people make the opposite mistake, they have a string and claim it to be an object... oh and I edited your example, because if you write it like I did you can see its structure much better. Commented Dec 13, 2013 at 8:54

3 Answers 3

7

Using alert() will not show you objects, this is one of the big advantages of the console. Check this fiddle and use the console (pressing F12 on your browser). Then you understand how to refer to what is inside that object.

var domains = {"DomID00001":{"ID":"DomID00001","Name":"Testdomein1"},"DomID00002":{"ID":"DomID00002","Name":"Testdomein2"}};

console.log(domains);
console.log(domains.DomID00001);
console.log(domains.DomID00001.ID);
Sign up to request clarification or add additional context in comments.

3 Comments

I'm afraid I don't see how this answers the question... Maybe I'm just misunderstanding the question... or your answer... =_=
@NiettheDarkAbsol, I think both your (which I upvoted alone apparently) and my answer give some light into how to retrieve data from the object. Now we both have to wait if OP already knew about object property /values and if some questions still are unanswered.
Also interesting, much gratitude for the help. I looked into this and indeed gave some insight on how to do things. But after all I went with the Dark Absols solution below. Still upvotes this.
1

Since the keys are variable, you should probably use a for..in loop:

for( domid in domains) if( domains.hasOwnProperty(domid)) {
    console.log(domid,domains[domid].ID,domains[domid].Name);
}

1 Comment

I used this approach in order to retrieve the values. Thanks for the help.
1

Try this:

var domains = {"DomID00001":{"ID":"DomID00001","Name":"Testdomein1"},"DomID00002":{"ID":"DomID00002","Name":"Testdomein2"}};

var strName1 = domains.DomID00001.Name;
var ID1 = domains.DomID00001.ID;
alert('Name: ' + strName1 + ' - ID: ' + ID1);

2 Comments

Works but too static for what I need. This into a for loop and perfecto ;D still +1 thanks!
I don't think for .. in is good about speed, you know! Just try something like JSON.parse(), stringify() ...etc. Thank you for voteup

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.