我正在尝试将对象数组(嵌套)转换为 JSON 字符串。这是我的 JSON 输出:
[{
"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
}]
当我尝试将此字符串转换为 XML(在服务器端)时,结果如下:
<?xml version="1.0" encoding="UTF-8" ?>
<0>
<Width>570</Width>
<SessionID>2003404006158805</SessionID>
<Price>69,90</Price>
<PageCount>24</PageCount>
<Pages>
<ID>1</ID>
<TemplateID>0</TemplateID>
<PageType>cover</PageType>
</Pages>
<Pages>
<ID>2</ID>
<TemplateID>1001</TemplateID>
<PageType>single</PageType>
</Pages>
<Pages>
<ID>3</ID>
<TemplateID>0</TemplateID>
<PageType>double</PageType>
<ImageContainers>
<Width>570</Width>
<IsBG>true</IsBG>
<Brightness>0</Brightness>
<Contrast>0</Contrast>
<PosX />
<ScaleX />
<Height>284</Height>
<ID>-1</ID>
<BlackWhite>0</BlackWhite>
<PosY />
<HasPhoto>false</HasPhoto>
<ScaleY />
<PhotoID />
</ImageContainers>
</Pages>
<Pages>
<ID>4</ID>
<TemplateID>0</TemplateID>
<PageType>double</PageType>
<ImageContainers>
<Width>570</Width>
<IsBG>true</IsBG>
<Brightness>0</Brightness>
<Contrast>0</Contrast>
<PosX />
<ScaleX />
<Height>284</Height>
<ID>-1</ID>
<BlackWhite>0</BlackWhite>
<PosY />
<HasPhoto>false</HasPhoto>
<ScaleY />
<PhotoID />
</ImageContainers>
</Pages>
<ProductSubID>0</ProductSubID>
<Height>620</Height>
<ProductID>0</ProductID>
</0>
但我需要它像:
<pages>
<page>
</page>
<page>
</page>
</pages>
这是我将对象数组转换为 JSON 的 AS 代码
var Pages:Array = [];
var Books:Array = [];
var JBook:Object = new Object();
JBook.Width = Global.BOOK_WIDTH;
for(var i:Number = 0; i<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<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<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<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);
知道为什么它会渲染 JSON 字符串,就像它们不在同一个节点中(每个页面项目的单独节点)吗?
希望我已经清楚了。谢谢。