title: AS3 Object To JSON
body: <p>I'm trying to convert an array of objects (nested) to JSON string.
  Here is my JSON output:</p>
  
  <pre><code>[{
      "Width": 570,
      "SessionID": 2003404006158805,
      "Price": "69,90",
      "PageCount": 24,
      "Pages": [{
          "ID": 1,
          "TemplateID": 0,
          "PageType": "cover",
          "TextContainers": [],
          "ImageContainers": []
      }, {
          "ID": 2,
          "TemplateID": 1001,
          "PageType": "single",
          "TextContainers": [],
          "ImageContainers": []
      }, {
          "ID": 3,
          "TemplateID": 0,
          "PageType": "double",
          "TextContainers": [],
          "ImageContainers": [{
              "Width": 570,
              "IsBG": true,
              "Brightness": 0,
              "Contrast": 0,
              "PosX": null,
              "ScaleX": null,
              "Height": 284,
              "ID": -1,
              "BlackWhite": 0,
              "PosY": null,
              "HasPhoto": false,
              "ScaleY": null,
              "PhotoID": null
          }]
      }, {
          "ID": 4,
          "TemplateID": 0,
          "PageType": "double",
          "TextContainers": [],
          "ImageContainers": [{
              "Width": 570,
              "IsBG": true,
              "Brightness": 0,
              "Contrast": 0,
              "PosX": null,
              "ScaleX": null,
              "Height": 284,
              "ID": -1,
              "BlackWhite": 0,
              "PosY": null,
              "HasPhoto": false,
              "ScaleY": null,
              "PhotoID": null
          }]
      }],
      "ProductSubID": 0,
      "Height": 620,
      "ProductID": 0
  }]
  </code></pre>
  
  <p>And when I'm trying to convert this string to XML (at server side) comes out like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
      &lt;0&gt;
          &lt;Width&gt;570&lt;/Width&gt;
          &lt;SessionID&gt;2003404006158805&lt;/SessionID&gt;
          &lt;Price&gt;69,90&lt;/Price&gt;
          &lt;PageCount&gt;24&lt;/PageCount&gt;
          &lt;Pages&gt;
              &lt;ID&gt;1&lt;/ID&gt;
              &lt;TemplateID&gt;0&lt;/TemplateID&gt;
              &lt;PageType&gt;cover&lt;/PageType&gt;
          &lt;/Pages&gt;
          &lt;Pages&gt;
              &lt;ID&gt;2&lt;/ID&gt;
              &lt;TemplateID&gt;1001&lt;/TemplateID&gt;
              &lt;PageType&gt;single&lt;/PageType&gt;
          &lt;/Pages&gt;
          &lt;Pages&gt;
              &lt;ID&gt;3&lt;/ID&gt;
              &lt;TemplateID&gt;0&lt;/TemplateID&gt;
              &lt;PageType&gt;double&lt;/PageType&gt;
              &lt;ImageContainers&gt;
                  &lt;Width&gt;570&lt;/Width&gt;
                  &lt;IsBG&gt;true&lt;/IsBG&gt;
                  &lt;Brightness&gt;0&lt;/Brightness&gt;
                  &lt;Contrast&gt;0&lt;/Contrast&gt;
                  &lt;PosX /&gt;
                  &lt;ScaleX /&gt;
                  &lt;Height&gt;284&lt;/Height&gt;
                  &lt;ID&gt;-1&lt;/ID&gt;
                  &lt;BlackWhite&gt;0&lt;/BlackWhite&gt;
                  &lt;PosY /&gt;
                  &lt;HasPhoto&gt;false&lt;/HasPhoto&gt;
                  &lt;ScaleY /&gt;
                  &lt;PhotoID /&gt;
              &lt;/ImageContainers&gt;
          &lt;/Pages&gt;
          &lt;Pages&gt;
              &lt;ID&gt;4&lt;/ID&gt;
              &lt;TemplateID&gt;0&lt;/TemplateID&gt;
              &lt;PageType&gt;double&lt;/PageType&gt;
              &lt;ImageContainers&gt;
                  &lt;Width&gt;570&lt;/Width&gt;
                  &lt;IsBG&gt;true&lt;/IsBG&gt;
                  &lt;Brightness&gt;0&lt;/Brightness&gt;
                  &lt;Contrast&gt;0&lt;/Contrast&gt;
                  &lt;PosX /&gt;
                  &lt;ScaleX /&gt;
                  &lt;Height&gt;284&lt;/Height&gt;
                  &lt;ID&gt;-1&lt;/ID&gt;
                  &lt;BlackWhite&gt;0&lt;/BlackWhite&gt;
                  &lt;PosY /&gt;
                  &lt;HasPhoto&gt;false&lt;/HasPhoto&gt;
                  &lt;ScaleY /&gt;
                  &lt;PhotoID /&gt;
              &lt;/ImageContainers&gt;
          &lt;/Pages&gt;
          &lt;ProductSubID&gt;0&lt;/ProductSubID&gt;
          &lt;Height&gt;620&lt;/Height&gt;
          &lt;ProductID&gt;0&lt;/ProductID&gt;
      &lt;/0&gt;
  </code></pre>
  
  <p>But I need it to be like:</p>
  
  <pre><code>&lt;pages&gt;
  &lt;page&gt;
  &lt;/page&gt;
  &lt;page&gt;
  &lt;/page&gt;
  &lt;/pages&gt;
  </code></pre>
  
  <p>This is my AS code to convert Object arrays into JSON</p>
  
  <pre><code>var Pages:Array = [];
              var Books:Array = [];
              var JBook:Object = new Object();
  
              JBook.Width = Global.BOOK_WIDTH;
  
              for(var i:Number = 0; i&lt;Global.PAGES.length; i++)
              {
                  var Page:PageVO = Global.PAGES[i] as PageVO;
                  var JPage:Object = new Object();
                  JPage.ID = Page.ID;
  
                  var ImageContainers:Array = [];
                  var TextContainers:Array = [];
                  var Template:TemplateVO = Page.ACTIVE_TEMPLATE;
  
                  for(var j:Number = 0; j&lt;Template.IMAGE_CONTAINERS.length; j++)
                  {
                      var ImageContainer:ImageContainerVO = Template.IMAGE_CONTAINERS[j] as ImageContainerVO;
                      var JImageContainer:Object = new Object();
                      JImageContainer.ID = ImageContainer.ID;
                      ImageContainers.push(JImageContainer);
                  }
  
                  for (var m:Number = 0; m&lt;Template.TEXT_CONTAINERS.length; m++)
                  {
                      var TextContainer:TextContainerVO = Template.TEXT_CONTAINERS[m] as TextContainerVO;
                      var JTextContainer:Object = new Object();
                      JTextContainer.ID = TextContainer.ID;
                  }
  
                  JPage.TextContainers = TextContainers;
                  JPage.ImageContainers = ImageContainers;
                  Pages.push(JPage);
              }
  
              var Photos:Array = [];
              for(var p:Number = 0; p&lt;Global.PHOTOS.length; p++ )
              {
                  var Photo:PhotoVO = Global.PHOTOS[p] as PhotoVO;
                  var JPhoto:Object = new Object();
                  JPhoto.BMP = ImageUtils.BitmapToBase64(Photo.BMP.bitmapData);
                  JPhoto.UseCount = Photo.USE_COUNT;
                  JPhoto.ID = Photo.ID;
                  Photos.push(JPhoto);
              }
  
  
              //JBook.Photos = Photos;
              JBook.Pages = Pages;
          JSON   = com.adobe.serialization.json.JSON.encode(Books);
  </code></pre>
  
  <p>Any idea why it's rendering JSON string like they are not in the same node (seperate node for every page item)?</p>
  
  <p>Hope I've been clear. Thanks.</p>