var Mary = '{ "height":1.9, "age":36, "eyeColor":"brown"}';
// use JSON.parse() to create an object 'objectMary':
var objectMary = JSON.parse(Mary);
Instructions:
I need to add an additional line after the existing code to log the value of Mary's age to the console.
What does this mean? How would I do this?
If I understand the question correctly, you would like to access Mary's age from the json.
var Mary = '{ "height":1.9, "age":36, "eyeColor":"brown"}';
var objectMary = JSON.parse(Mary);
Here, objectMary has the parsed json object and you can directly access properties from it like objectMary.age, objectMary.height, etc.
If you are trying this code out in javascript then you could log Mary's age as
var Mary = '{ "height":1.9, "age":36, "eyeColor":"brown"}';
var objectMary = JSON.parse(Mary);
console.log(objectMary.age);
Just add console.log(objectMary.age); in your existing code.
DEMO
var Mary = '{ "height":1.9, "age":36, "eyeColor":"brown"}';
var objectMary = JSON.parse(Mary);
console.log(objectMary.age);
Related
I tried to retrieve the variable Default from json of people api but it doesn't work with "default" . how to do it ? thank you for your answer
see my test script below, please (last line at bottom)
var person = People.People.get('people/' + accountId, {personFields: 'names,photos,phoneNumbers,addresses,birthdays,sipAddresses,organizations,genders'});
//** var val_displayName_P = person.names[0].displayName;
var val_photoUrl = person.photos[0].url.replace("=s100","=s128");
var primary_BLN = person.photos[0].metadata.primary;
var default_BLN = person.photos[0].default;
Seems like it is not returning default field in JSON response when a particular accountID contains user-provided photo(default : false), looks like a bug to me, not sure.
In case of User-provided photo, this is the log i am getting :-
And in case of default photo:-
Maybe that's why execution is breaking at last line.
If this is the case, for now you can try this :-
const photo= person.photos[0]
var default_BLN = photo.hasOwnProperty('default') ? photo.default : false;
Reference:
Photo
JSON is case sensitive, try person.photos[0].Default.
I'm a beginner ok... I'm trying to get multiple values of a cell memberEmail, then insert to mail group.
But I got an error is Invalid Input: memberKey.
var groupMail = 'test#gmail.com';
var listUserMails = [];
var userMails = JSON.stringify(sheet.getRange(4, 9).getValue());
listUserMails = userMails.split("\\n");
for (var i = 0; i < listUserMails.length; i++) {
var userEmail = listUserMails[i];
var member = {
email: userEmail,
role: 'MEMBER'
};
member = AdminDirectory.Members.insert(member, groupMail);
}
Update:
When I log value of an array listMemberMails, it seems that the array I received contains only 1 string. It's not the 3 strings as expected.
["nguyen_tien_nghiep#gmail.com, nguyen_hai_ninh#gmail.com, vu_xuan_lam#gmail.com"]
<blockquote class="imgur-embed-pub" lang="en" data-id="a/hMd1ckO"></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
Your problem comes from the JSON.stringify function. It assigns to the variable userMails the value
"nguyen_tien_nghiep#gmail.com\n nguyen_hai_ninh#gmail.com\n vu_xuan_lam#gmail.com"
whereby the doublequotes are considered part of the string. This is why, if you split userMails in three, it will give you the following three array entries:
listUserMails[0]: "nguyen_tien_nghiep#gmail.com
listUserMails[1]: nguyen_hai_ninh#gmail.com
listUserMails[2]: vu_xuan_lam#gmail.com"
You can easily confirm the latter by adding Logger.log(userEmail) inside of your for-loop. This is what inhibits you from creating three members with a valid email address (since only the second entry is a valid email address.
With this being said if you replace
var userMails = JSON.stringify(sheet.getRange(4, 9).getValue());
with
var userMails = sheet.getRange(4, 9).getValue();
and then adjust the separator to "\n" instead of "\n" your code should work correctly.
As a suggestion, you can debug your code by inserting logs (e.g. for listUserMails.length). This will help you to find errors.
Paypal IPN message format is kind weird where spaces are "&", # becomes "%40" and so on and I do not know what do you call that format.
I tried replacing those characters like this:
var set0 = receipt.split('&').join(',&');
var set1 = set0.replace(/&/g,'\n');
var set2 = set1.replace(/%40/g,'#');
var set3 = set2.replace(/%2C/g,',');
var set4 = set3.replace(/%3A/g,':');
var set5 = set4.split('+').join(' ');
var set6 = set5.split('=').join(': ');
var test = JSON.parse(JSON.stringify(set6));
console.log(test);
I got the exact arrangement that I want by replacing those characters:
amount1: 1.00,
amount3: 14.99,
address_status: confirmed,
subscr_date: 17:41:18 Aug 07, 2016 PDT,
payer_id: NZKP3G9KN3Y5G,
address_street: 1 Main St,
mc_amount1: 1.00,
mc_amount3: 14.99
But the problem is I can not get the value let say:
console.log(test.amount1);
It says invalid. Please advise thank you.
This is the sample IPN Post back I get from Paypal sandbox:
amount1=1.00&amount3=14.99&address_status=confirmed&subscr_date=17%3A41%3A18+Aug+07%2C+2016+PDT&payer_id=NZKP3G9KN3Y5G&address_street=1+Main+St&mc_amount1=1.00&mc_amount3=14.99&charset=windows-1252&address_zip=95131&first_name=test&reattempt=1&address_country_code=US&address_name=test+buyer¬ify_version=3.8&subscr_id=I-LYDJAAW6R5J0&custom=QuQHkL8EQ2XLYD6Tz&payer_status=verified&business=billing-facilitator%40testmail.com&address_country=United+States&address_city=San+Jose&verify_sign=AiPC9BjkCyDFQXbSkoZcgqH3hpacAm05VARKo4Q3kMeDNMwELG8cel.7&payer_email=billing-buyer-2%40testmail.com&btn_id=3394935&last_name=buyer&address_state=CA&receiver_email=billing-facilitator%40testmail.com&recurring=1&txn_type=subscr_signup&item_name=Premium+with+Trial&mc_currency=USD&residence_country=US&test_ipn=1&period1=1+D&period3=1+M&ipn_track_id=1e857dd5b422e
And I used replace method (the code above) to get the desired format.
I want to be able to add an "Ignore List" with the results being saved on the users browser.
The Ignored List is saved as a JSON array and looks like this:
[{"username":"test_user","date_added":"19/08/13","description":"Don't like this person."},{"username":"test_user_2","date_added":"19/08/13","description":"Don't like this person."}]
And the function required to add the users look like this:
function add_to_ignore_list()
{
var ignored_users = localStorage.getItem("ignore_list"); // returns ignore list
var username = return_current_username(); // returns test_user3
var date = return_current_date(); // returns 19/08/13
var description = prompt("Why do you want to ignore this user?"); // returns desc
add_to_list = {
"username" : username,
"date_added" : date,
"description" : description
};
ignored_users.push(add_to_list);
localStorage["ignore_list"] = JSON.stringify(ignored_users);
$(".user_wrapper").css("background-color","#B40404");
}
For some reason it isn't working and I can't see why Please help.
ignored_users is stored as a string.
When you retrieve it from localStorage, you need to parse it before you use it.
change:
var ignored_users = localStorage.getItem("ignore_list");
to (assumes it had previously been stored):
var ignored_users = JSON.parse(localStorage.getItem("ignore_list"));
I am pulling data from a php script that gives me the names of a person on facebook along with there ID in this format:
Person Name:123456789
I would like to know if there is a away to split after the ":" and add the number (ID) to on array and the person name to another array.
Thanks for any help
Eli
Something like this:
var personArray:Array = [];
var idArray:Array = [];
var stringToSplit:String = "Person Name:123456789";
var splitArray:Array = stringToSplit.split(":");
personArray.push(splitArray[0]);
idArray.push(splitArray[1]);
trace(personArray); // outputs "Person Name"
trace(idArray); // outputs "123456789"