Sample code for 30+ languages & platforms
Java Requires Chilkat v11.4.0+

Example: JsonObject.NewObjectOf method

Demonstrates the NewObjectOf method.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        String car = "{\"make\":\"Toyota\",\"model\":\"Camry\",\"year\":2022,\"color\":\"silver\",\"engine\":{\"type\":\"inline-4\",\"fuel\":\"gasoline\",\"horsepower\":203},\"features\":[\"bluetooth\",\"backup camera\",\"adaptive cruise control\",\"lane assist\"],\"isElectric\":false}";

        JsonObject json = new JsonObject();

        json.updateString("test", "abc");

        try {
            json.newObjectOf("car", car);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        json.setEmitCompact(false);
        System.out.println(json.emit());

        // Result:

        // {
        //   "test": "abc",
        //   "car": {
        //     "make": "Toyota",
        //     "model": "Camry",
        //     "year": 2022,
        //     "color": "silver",
        //     "engine": {
        //       "type": "inline-4",
        //       "fuel": "gasoline",
        //       "horsepower": 203
        //     },
        //     "features": [
        //       "bluetooth",
        //       "backup camera",
        //       "adaptive cruise control",
        //       "lane assist"
        //     ],
        //     "isElectric": false
        //   }
        // }
    }
}