/u/vonadler gave a great answer to a similar question [here] (https://www.reddit.com/r/AskHistorians/comments/5ty46r/why_do_hitler_choose_not_to_take_sweden_in_world/). In short it's not so much Swedens diplomatic efforts or military detarrance, it's the simple fact that Nazi Germany had no strategic or ideological reason to invade.
Edit: I didn't notice at first you asked about both world wars but the link above does at least cover WW2.