Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this json data from the server

{

"store":[{"skey":"asdsad123das","name":"Store1"},  {"skey":"3308d976d2647659f130d09593be845a","name":"joji store"}],

"manager":[{"mkey":"asdsad123das","name":"manager 1"},{"mkey":"asdsad123da2","name":"Manager 2"},{"mkey":"asdsad123da3","name":"Manager 3"}]

}

i want to read that data . when i try to read it , it always give me undifined . i am doing something like this

 success: function(msg){

            //alert(msg['store'].name);
            for (var keys in msg)
            {
                    //alert(keys);
                    alert(keys['name']);
                    for (var data in keys)
                    {
                            alert(data);

                    }
            }
      },
share|improve this question
    
Well you should show how you retrieve the data. The data is send as string from the server and needs to be parsed first. If you use a library to request the data this normally should do it automatically for you (if the response header is correct and/or you specify that you request json). –  t.niese Jul 13 '13 at 19:37
    
Please provide the code you wrote to try to read the data. That will help determine what you are doing wrong. Thanks! –  Michal Jul 13 '13 at 19:37
    
@Michal thanks for the reply i updated the code which i am using to retrieve the data –  user1224233 Jul 13 '13 at 20:09
    
Thanks! That was very helpful. See my answer for details, and let me know if you need more clarification in comments on it. –  Michal Jul 13 '13 at 20:23
add comment

2 Answers

up vote 2 down vote accepted

In general, when you have an object with named keys (obtained from JSON data or elsewhere), you're not likely to want to iterate over those keys. It's more likely that you will want to reference those keys specifically by name. After all, a store is a very different thing from a manager, isn't it?

In most cases it wouldn't make much sense to write a loop (or equivalent such as $.each()) where you iterate over both stores and managers and do the same thing with them. You probably want to do one thing with stores, and a different thing with managers.

OTOH, with the specific data you have here, a store record does look a lot like a manager record. Each one has a name; the difference is that a store has an skey and a manager has an mkey. So just for the purposes of logging your data, you could do something like this (fiddle):

var data = {
    "store": [
        { "skey": "asdsad123das", "name": "Store1" },
        { "skey": "3308d976d264", "name": "joji store" }
    ],
    "manager":[
        { "mkey":"asdsad123das", "name":"manager 1" },
        { "mkey":"asdsad123da2", "name":"Manager 2" },
        { "mkey":"asdsad123da3", "name":"Manager 3" }
    ]
};

function success( data ) {
    list( data.store, 'skey' );
    console.log( ' ' );
    list( data.manager, 'mkey' );
    function list( array, key ) {
        $.each( array, function( i, item ) {
            console.log( item[key], ':', item.name );
        });
    }
}

success( data );

Note that we use data.store and data.manager to access each of the two individual arrays. (And I called it data instead of msg because msg sounds like an abbreviation for "message", and this does not seem to be a message of any kind.)

But since the arrays themselves are similar, I used a common function to log each of them. That function takes a parameter telling it whether to use the skey or mkey.

In real code working with this data, you may find that what you do with a manager is so different from what you do with a store that you won't even have this much common code between them. But once you're able to access that data reliably you can figure it out from there.

Now the most important tip: Do you know how to use the JavaScript debugger in your favorite browser? I recommend the one built into Chrome, but there is also FireBug for Firefox and the built-in debugger in Internet Explorer.

If you add a debugger; statement to your code and load your page with the debugger open, it will stop on that line of code, and then you can look at the variables in the locals/watch panels, and you can use the console to try out different expressions to see what they do. You can also single-step through your code, step into functions, etc. You won't need to use alert() any more; you'll find this a much superior way to debug your code.

Here is information on the Chrome developer tools. If you haven't used these debugging tools before, you should seriously stop everything else you're doing and get familiar with them. It will pay off immensely.

share|improve this answer
    
I am really so much thankful to you , thats where i stuck and you describe it so nicely . I will surely start using the debugger . Thanks alot for your tips and sharing information in so much details :) –  user1224233 Jul 13 '13 at 22:44
1  
Excellent, I'm glad that was useful. You will really like the way the Chrome debugger helps you track down problems and gain insight into both your own code and other websites. –  Michael Geary Jul 14 '13 at 4:13
add comment

You are accessing the JSON like you would a dictionary or array. JSON works differently. Let's assume we are storing our JSON in a variable we call data, like so:

var data = {
  "store": [ {
    "skey": "asdsad123das",
    "name": "Store1"
  }, {
    "skey": "3308d976d2647659f130d09593be845a",
    "name": "joji store"
  } ],
  "manager": [ {
    "mkey": "asdsad123das",
    "name": "manager 1"
  }, {
    "mkey": "asdsad123da2",
    "name": "Manager 2"
  }, {
    "mkey": "asdsad123da3",
    "name": "Manager 3"
  } ]
};

Then we can access specific keys like so:

data.store[0].skey     // skey of first element of store array of data object

If you want to iterate over the data in data, you can do:

for (index in data.store) {
  console.log(data.store[index]);       // prints whole object
  console.log(data.store[index].skey);  // prints skey
  console.log(data.store[index].name);  // prints name
}

Example in this fiddle: http://jsfiddle.net/u4HzZ/2/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.