I have a json file that holds data in this form:
{
"UserEmail": "email@domain.com",
"UserAvatar": "url",
"Username": "Name",
"Contacts": [
"email2@domain.com",
"emai3l@domain.com"
],
"MyFriends": [
"",
""
],
"MyEnemies": [
"",
""
],
"FriendsAvatar": {
"email2@domain.com": "url2",
"email3@domain.com": "url3"
}
}
I want to pass the data to js array so when I need a value to call the appropriate key. For array_json['UserEmail']
I'll get email@domain.com
as a result and for array_json['Contacts']
another array with contact infos.
I search the past question here, but whenever I tried to use $.getJSON()
JQuery function I get errors.
Here is what I think:
var obj = [];
$.ajax({
url: "json/HV.json",
success: function (data) {
obj = JSON.parse(data);
alert(obj);
}
});
You dont need to use parse function. It can be accessed as below
$.ajax({
url: "json/HV.json",
success: function (data) {
alert(data.UserEmail);
}
});
See it is working in this snippet
var data = { "UserEmail": "email@domain.com", "UserAvatar": "url", "Username": "Name", "Contacts": [ "email2@domain.com", "emai3l@domain.com" ], "MyFriends": [ "", "" ], "MyEnemies": [ "", "" ], "FriendsAvatar": { "email2@domain.com": "url2", "email3@domain.com": "url3" } } alert(data.UserEmail)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.