4
$.getJSON(
    "test.php",
    function(data){
        ... code should be here?
    }
)

data contains this code:

{
    "name": "Mary",
    "surname": "Carey"
}

I want to create these variables:

theName = name from json;
theSurname = surname from json;

What is a true syntax for this task?

Thanks.

3

3 Answers 3

3

Dot notation:

theName = data.name;
theSurname = data.surname;

or square-bracket notation:

theName = data['name'];
theSurname = data['surname'];
Sign up to request clarification or add additional context in comments.

Comments

2

You could do:

theName = data.name
theSurname = data.surname

… but it is probably better to keep them nicely wrapped up in data and just use that.

Comments

0

The "data" should be a Javascript Object. If that is truly the data, you should be able to access it with data['name'] and data['surname'].

'dot' syntax should also work. (i.e. data.name and data.surname)

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.