JQuery json decode

$.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 the true syntax for this task?

Thanks.

+4
source share
3 answers

Spot Recording:

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

or square bracket:

 theName = data['name']; theSurname = data['surname']; 
+3
source

You can do:

 theName = data.name theSurname = data.surname 

... but it's probably best to keep them beautifully wrapped in data and just use that.

+2
source

"Data" must be a Javascript Object. If this is really data, you should have access to it with data ['name'] and data ['surname'].

Syntax

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

0
source

Source: https://habr.com/ru/post/1338129/


All Articles