Sample code for 30+ languages & platforms
Java

Using JSON StringOf with Non-String Members

See more JSON Examples

If a JSON member is a boolean, integer, or null, using StringOf will return its string representation.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // Create JSON with members of different data types.
        JsonObject json = new JsonObject();

        json.updateInt("a", 123);
        json.updateBool("b", true);
        json.updateNull("c");

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

        // Resulting JSON:
        // {
        //   "a": 123,
        //   "b": true,
        //   "c": null
        // }

        String a = json.stringOf("a");
        System.out.println(a);

        String b = json.stringOf("b");
        System.out.println(b);

        String c = json.stringOf("c");
        System.out.println(c);

        // Output

        // 123
        // true
        // null

        // If you want to get the integer, boolean, or null value
        // you need to use the methods matching the data type
        int ival = json.intOf("a");
        boolean bval = json.boolOf("b");
        boolean hasNull = json.isNullOf("c");
    }
}