$.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.
Dot notation:
theName = data.name;
theSurname = data.surname;
or square-bracket notation:
theName = data['name'];
theSurname = data['surname'];
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.
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)
精彩评论