Check out "Do you speak JavaScript?" - my latest video course on advanced JavaScript.
Language APIs, Popular Concepts, Design Patterns, Advanced Techniques In the Browser

Trace JSON object in AS3

I'm absolutely sure that all of you are working with JSON objects. It's pretty helpful to be able to print them and find out what exactly they contain. I wrote a simple function that makes this possible.

Here is the JSON object that we are going to use and the calling of the print method:

var arr:Array = [	{		categories:["a", "b", -12, "c"],		users:["John", "Steve"],		id:23,		information:{			name:"...name...",			description:"...description..."		}	},	{		categories:["d", "e"],		users:["Martin", "Jason"],		id:103,		information:{			name:"NNN",			description:"DDD"		}	}];printJSON(arr);

Here is the function printJSON

function printJSON(o: Object): void {
    trace("Debug.printJSON");
    trace(parseJSON(o));
  }
  
  function parseJSON(o: * , spaces: int = 1): String {
    var str: String = "";
    if (getTypeof(o) == "object") {
      str += "{\\n";
      for (var i: * in o) {
        str += getSpaces(spaces) + i + "=";
        if (getTypeof(o[i]) == "object" || getTypeof(o[i]) == "array") {
          str += parseJSON(o[i], spaces + 1) + "\\n";
        } else {
          var type: String = getTypeof(o[i]);
          if (type == "string") {
            str += "\\"
            " + o[i] + "\\
            "\\n";
          } else if (type == "number") {
            str += o[i] + "\\n";
          }
        }
      }
      str += getSpaces(spaces - 1 < 0 ? 0 : spaces - 1) + "}";
    } else if (getTypeof(o) == "array") {
      str += "[\\n";
      var n: int = o.length;
      for (i = 0; i < n; i++) {
        str += getSpaces(spaces) + "[" + i + "]=";
        if (getTypeof(o[i]) == "object" || getTypeof(o[i]) == "array") {
          str += parseJSON(o[i], spaces + 1) + "\\n";
        } else {
          type = getTypeof(o[i]);
          if (type == "string") {
            str += "\\"
            " + o[i] + "\\
            "";
          } else if (type == "number") {
            str += o[i];
          }
          str += "\\n";
        }
      }
      str += getSpaces(spaces - 1 < 0 ? 0 : spaces - 1) + "]";
    }
    return str;
  }
  
  function getSpaces(n: int): String {
    var str: String = "";
    for (var i: int = 0; i < n; i++) {
      str += "  ";
    }
    return str;
  }
  
  function getTypeof(o: * ): String {
    return typeof (o) == "object" ? (o.length == null ? "object" : "array") : typeof (o);
  }

And the result is:

Debug.printJSON[  [0]={    information={      name="...name..."      description="...description..."    }    users=[      [0]="John"      [1]="Steve"    ]    id=23    categories=[      [0]="a"      [1]="b"      [2]=-12      [3]="c"    ]  }  [1]={    information={      name="NNN"      description="DDD"    }    users=[      [0]="Martin"      [1]="Jason"    ]    id=103    categories=[      [0]="d"      [1]="e"    ]  }]
If you enjoy this post, share it on Twitter, Facebook or LinkedIn.