0

Details :

Have a good day programmers. My server is returning me the multidimensional json array.Now i just want to access it individually and also i am little bit confused in JSON format.

Question 1: what is the difference between these Type1 and Type2 json format?

JSON Type 1

{
    "name": "Stackoverflow",
    "exp": "4month",
    "status": 
             {
               "username": "koushik",
               "password": "mypassword"
             }
}

JSON Type 2

{
    "name": "Stackoverflow",
    "exp": "4month",
    "status": [
        {
            "username": "koushik",
            "password": "mypassword"
        }
    ]
}

I know both are valid json format but where it differ's?

Question 2: Using ajax i can handle single dimention json data

Example: It works

$.ajax({
        url:"temp.json",
        dataType:"json",
        success:function(data){     
                 alert(data.name);
        }
    }); 

In the same way how could i handle multidimensional json array.Example i want to get something like this alert(data=>status=>username) . I know it is very easy to do but i am struck here. Thanks in advance.

1
  • That's not multi-dimensional. It's got one dimension. Commented Mar 12, 2014 at 11:33

3 Answers 3

4

Question 1: what is the difference between these Type1 and Type2 json format?

In the first, the status property refers to an object (that in turn has two properties). In the second, the status property refers to an array, the first and only element of which is an object (that in turn has two properties).

Question 2:

For your "Type1" you can just do:

data.status.username

For your "Type2":

data.status[0].username

...or if a real-world case of "Type2" had an array with more objects in it you could loop over the items and do something with them:

for(var i = 0; i < data.status.length; i++) {
    console.log(data.status[i].username);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need something like this -

$.each(data.status,function(i,v){
   console.log(v.username);
});

Comments

1
if($.isArray(data.status)){
  // JSON Type 2
  $(data.status).each(function(){console.log(this.username);});

}
else
{
 //JSON Type 1
 console.log(data.status.username);
}

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.