开发者

jQuery json decode

开发者 https://www.devze.com 2023-02-08 08:02 出处:网络
$.getJSON( \"test.php\", function(data){ ... code should be here? } ) 开发者_高级运维data contains this code:
$.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)

0

精彩评论

暂无评论...
验证码 换一张
取 消