Conclusion: Using Ali’s Fastjson package, you can easily convert to and from each other. The introduction of pom

<! Alibaba </groupId> <artifactId>fastjson</artifactId> </dependency>Copy the code

Here’s a concrete example:

  • The entity class ReqCheckAccount inherits From ReqMain, which has only a few common attributes. Let’s paste the ReqCheckAccount class directly
public class ReqCheckAccount extends ReqMain {


    private String account = "";/ / account
    private String accountName = "";// Account name

    public String getAccount(a) {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getAccountName(a) {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    @Override
    public String toString(a) {
        return "ReqCheckAccount{" +
                "account='" + account + '\' ' +
                ", accountName='" + accountName + '\' ' +
                ", id='" + id + '\' ' +
                ", appId='" + appId + '\' ' +
                ", bankId='" + bankId + '\' ' +
                ", jgxybh='" + jgxybh + '\' ' +
                '} '; }}Copy the code
  • The test class

Here is the Test (org. Junit. Jupiter. API. Test) annotations, create a Test class directly, use the Test annotations can be:

@Test
    public void testJSON(a){
        ReqCheckAccount reqCheckAccount = new ReqCheckAccount();
        String strJson = JSONObject.toJSONString(reqCheckAccount);
        System.out.println("Object to JSON test:"+strJson);

        strJson = "{\"account\":\"111\",\"accountName\":\"222\",\"appId\":\"333\",\"bankId\":\"444\",\"id\":\"555\",\"jgxybh\":\"666\"}";
        ReqCheckAccount reqObj = (ReqCheckAccount) JSONObject.toJavaObject(JSON.parseObject(strJson),ReqCheckAccount.class);
        System.out.println("JSON to Object:"+reqObj.toString());
    }
Copy the code

The results of