10.10.2 Encoding JSON

Use the haxe.Json.stringify static method to encode a Haxe value into a JSON string:

class Main {
  static function main() {
    var o = {rating: 5};
    var s = haxe.Json.stringify(o);
    trace(s); // {"rating":5}
  }
}

Pretty print JSON

If the parameter space is given, the result will be pretty-printed. Successive levels will be indented by this string.

class Main {
    static function main() {
        var o = {rating: 5, title: "Haxe"};
        var s = haxe.Json.stringify(o, "\t");
        trace(s);
        /* {
            "rating": 5,
            "title": "Haxe"
        } */
    }
}