0

I'am doing a jquery call to API website, which returns me the results in JSON format:

{
  "results":[
    {
      "user":{
        "gender":"female",
        "name":{
          "title":"mrs",
          "first":"linda",
          "last":"diaz"
        },
        "location":{
          "street":"2333 oak lawn ave",
          "city":"red bluff",
          "state":"maryland",
          "zip":"49309"
        },
        "email":"[email protected]",
        "password":"blackman",
        "md5_hash":"3c64b82d048c8754a30e292a1359fa39",
        "sha1_hash":"d5095cf146dda75865d348f4ce4820b11b58b9fd",
        "phone":"(880)-878-1658",
        "cell":"(183)-179-1598",
        "SSN":"425-55-1070",
        "picture":"http:\/\/api.randomuser.me\/0.2\/portraits\/women\/8.jpg"
      },
      "seed":"2d589586d34c1c5",
      "version":"0.2.1"
    }
  ]
}

How can I access (or get values of) the items, for example: I want to console.log() the first name and the last name, get a phone number ?

Using a .(dot) not working for me, maybe i'am doing something wrong ? Here is a javascript code

$.ajax({
    type: 'POST',
    url: url + resultsQuery,
    dataType: 'json',
    success: function(data){
        console.log(data);
    }
});
9
  • it's an array, so you'll need a combination of array notation and dot notation. Can you show us what you have tried? Commented Nov 15, 2013 at 16:28
  • i tried to use console.log(data.results[0].user) but it returns me the undefined Commented Nov 15, 2013 at 16:29
  • 1
    if that returns undefined, the json isn't structured the way you think it is, that should have given you an object. Are you sure a user is getting returned at all? Commented Nov 15, 2013 at 16:29
  • @KevinB - it means, I'am doing something wrong or the JSON is not actually a real JSON format ? Commented Nov 15, 2013 at 16:30
  • 1
    console.log(data.results.length) Commented Nov 15, 2013 at 16:34

2 Answers 2

2
data.results[0].user.name.first
data.results[0].user.name.last
data.results[0].user.phone
Sign up to request clarification or add additional context in comments.

Comments

2

For your JSON Structure, try

 data.results[0].user.name.first
 data.results[0].user.name.last //etc

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.