This is a two part question.
I would like to know how Japan resisted communist pre-1931 and post-1945.
I'm less familiar with pre-war Japan, so I'll leave that to someone else, but I'll address the environment in immediate post-war Japan.
The post-war occupation of Japan under SCAP, led by MacArthur, was very hands-on in guiding what ideologies and activities were permissible. In particular, Communist and leftist movements were very much not desirable in the view of the Americans at SCAP. One branch under SCAP, the Civil Censorship Detachment (CCD), imposed strict rules on what kind of content was permitted to be published and circulated, in general blocking anything that was damaging to the image of SCAP and anything that was too left-wing. In principle this censorship was gradually lifted starting from 1947 and formally was ended in 1949, but in reality censorship continued in other forms. In fact, as more liberal members left SCAP (becoming disillusioned with its actions that were not in line with promoting a free and truly democratic society), censorship became even stricter and more arbitrary. And while technically censorship controls were loosened by changing from pre-publication censorship (where material must be approved before it can be published) to post-publication watchlists (where papers are just monitored for unsavory content), this ended up having a chilling self-censorship effect on publishers and writers, as having your material censored after publication could be financially disastrous. With the censors often behaving seemingly arbitrarily (from the Japanese perspective) in choosing what they strike down, writers would err on the side of caution and give a wide berth to anything that could potentially be construed as too radical. American officials could also demand writers and editors that had displeased them be fired. Though this usually didn't come with an explicit military tribunal in the style of the first purges of the occupation, the threat that this could happen meant that people would usually resign upon request.
Besides censorship, SCAP also took direct action to stop movements they saw as too left-wing. Perhaps the most iconic example of this was the general strike that Japanese workers planned for early 1947, in protest of wages that were so low that the national railway employees' union calculated their average salary could only cover a quarter of typical family expenses. This planned strike had widespread support from unions with both communist and anti-communist leanings, totaling some 3 million workers. However, when negotiations between the union and the Japanese government broke down and it seemed inevitable the strike would begin the next day, MacArthur stepped in and dictated that the strike be cancelled, much to the delight of conservative leaders.
The outbreak of the Korean War helped escalate the purge of Communist sympathizers as well. In the weeks before the war began, MacArthur ordered the purge of the entire Central Committee of the Communist Party, as well as the top 17 editors of the official Japanese Communist Party paper. Once the war began, SCAP ordered this paper to cease publishing indefinitely, and over the next few months they shut down over a thousand Communist and left-wing papers. In addition, the Japanese government had already been carrying out a Red purge against public employees in late 1949 with the active cooperation of SCAP. This purge spilled into the private sector and all forms of media once the war began, with many studios and companies ordered to expel all Communists in their employ.
The summary here is that there was heavy-handed American censorship of Communist publications and heavy American support for the conservative government and its actions against the Communists. While Socialists and Communists continued to serve in Japanese government after the American occupation, and while they did play significant roles in debates about public policy, the legacy of the American occupation established a conservative hegemony in government and business that persisted to the end of the century.
Source: John Dower, Embracing Defeat