Java Spring Boot Pojo to Json

Viewed 48

I usually convert from POJO to JSON by using org.json.JSONObject. It always works very well, but I don't know why it's not working when I apply it in Spring Boot. Here are my 2 files:

src/main/java/oms/models/Person.java:

public class Person {

    private String name;
    private int age;

    public String getName() { return this.name; }
    public void setName( String name ) { this.name = name; }

    public int getAge() { return this.age; }
    public void setAge( int age ) { this.age = age; }

}

src/test/java/oms/Test.java:

import org.json.JSONObject;
import oms.models.Person;
import org.junit.jupiter.api.Test;

public class Test{

    @Test
    void Test {
        Person person = new Person();
        person.setName( "Person Name" );
        person.setAge( 333 );

        
        JSONObject jsonObj = new JSONObject( person );
        System.out.println( jsonObj );
    }

}

Then I'm getting an error: Cannot resolve constructor 'JSONObject(Order)'

Can someone let me know what is the problem?

0 Answers
Related