-3

I am trying to get an object from inside a JSON but I can't. It only gives me undefined.
But when I check the result on the console it gives me the right results.

Here is my JSON

{
    users: [
            {
            userID: "151",
            userFirstname: "first",
            userMiddlename: "middle",
            userLastname: "latname",
            userAddress: "qewasd",
            userContactNumber: "123456",
            userRole: "role",
            userName: "user",
            userPassword: "pass",
            userEmail: "test@gmail.com",
            userPitch: "Very good",
            userExperience: "5",
            userFirm: "Sample",
            userRollNo: "0"
        }
    ]
}

and here is my javascript

$(document).ready(function () {

    $.getJSON('http://batz.web/Sandbox/details.php?userID=151', function (results){
        document.getElementById("title").innerHTML = results.userID;
        console.log(results);
    });

});
theAlexandrian
  • 870
  • 6
  • 18
Baaaaaaatz
  • 13
  • 3
  • 6
    It should be `results.users[0].userID` – H77 Feb 18 '18 at 08:55
  • 4
    That's not JSON. (If it *were* JSON, it would be invalid JSON.) JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Feb 18 '18 at 08:55
  • 1
    @H77 - Wow, that worked! Thanks! – Baaaaaaatz Feb 18 '18 at 08:58

1 Answers1

0

Ok look at your json, userID is not a direct child of the results object, which means
It will always return null every time you try to get the value of undefined field

You first need to get the array's content with the key(left side of full colon), as the array is in "users" field(or key) ,userId is not still in the array's field, so you gotta point with an index(e.g,0,1,2,...) to get the object which contain userID

...
    document.getElementById("title").innerHTML = results.users[0].userId;
...

Here is the fiddle