现在的项目多数以数据去驱动。这样可以适应多种设备的调用。而在这里json格式是比较通用的。而在官方文档也说明了如何去转换你的json数据。(www.playframework.org/documentation/2.0.2/JavaJsonRequests) 而我在这里,是要和大家介绍更多的关于特殊情况的处理。如以下数据:
{ "introImg": "/assets/images/main/gdouIntro.jpg", "catalogName": "华师在线", "website": "http://www.gdou.com", "catalogID": 10000, "titileImg": "#", "mobileList": [{ "deviceCSS": "device device_iphone", "viewCSS": "view view_iphone", "minCSS": "minimg minimg_iphone", "deviceID": 500, "mobileIntro": "暂无", "showImg": "/assets/images/device/iphone/gdou/1.jpg", "showCSS": "display display_iphone", "downloadUrl": null, "deviceImg": "/assets/images/device/iphone.png", "bgCSS": "phone_bg", "menuCSS": "device_item item_iphone", "imgList": [{ "itemID": "5005", "picUrl": "/assets/images/device/iphone/gdou/1.jpg", "picID": 155 }, { "itemID": "5005", "picUrl": "/assets/images/device/iphone/gdou/2.jpg", "picID": 156 }, { "itemID": "5005", "picUrl": "/assets/images/device/iphone/gdou/3.jpg", "picID": 157 }, { "itemID": "5005", "picUrl": "/assets/images/device/iphone/gdou/4.jpg", "picID": 158 }, { "itemID": "5005", "picUrl": "/assets/images/device/iphone/gdou/5.jpg", "picID": 159 }], "deviceName": "iphone", "mobileID": 5005, "mobileName": "华师在线(iphone)", "bgImg": "/assets/images/device/phone_bg.jpg" },
json数组中某字段包含了另一个数组。我们必须要用一下方法处理:
//创建一个输出结果的容器
ArrayList jsonList= new ArrayList();
//从数据层获取数据
List catalogList=GdouCatalog.GetCatalogByID(catalogID);
//遍历所获得的数据
for(GdouCatalog item :catalogList)
{
Map
node.put("catalogID",item.catalogID);
node.put("catalogName",item.name);
node.put("intro",item.intro);
node.put("website",item.website);
node.put("logoImg",item.logoImg);
node.put("titileImg",item.titleImg);
node.put("introImg",item.introImg);
//二次遍历
ArrayList jsonMobileList= new ArrayList();
for(GdouMobileItem mobile:item.itemList)
{
Map
GdouMobile mobileInfo=GdouMobile.GetMobileInfo(mobile.mobileID);
mobileNode.put("deviceID",mobile.mobileID);
mobileNode.put("deviceName",mobileInfo.mobileName);
mobileNode.put("menuCSS",mobileInfo.menuCSS);
mobileNode.put("showCSS",mobileInfo.showCSS);
mobileNode.put("bgCSS",mobileInfo.bgCSS);
mobileNode.put("bgImg",mobileInfo.bgImg);
mobileNode.put("deviceImg",mobileInfo.deviceImg);
mobileNode.put("deviceCSS",mobileInfo.deviceCSS);
mobileNode.put("viewCSS",mobileInfo.viewCSS);
mobileNode.put("minCSS",mobileInfo.minCSS);
mobileNode.put("mobileID",mobile.itemID);
mobileNode.put("mobileName",mobile.name);
mobileNode.put("mobileIntro",mobile.intro);
mobileNode.put("downloadUrl",mobile.downloadUrl);
ArrayList imgList= new ArrayList();
int count=0;
for(GdouMobileImg img :mobile.imgList)
{
Map
imgNode.put("picID",img.picID);
imgNode.put("picUrl",img.picUrl);
imgNode.put("itemID",img.itemID);
imgList.add(imgNode);
if(count==0)
{
mobileNode.put("showImg",img.picUrl);
}
count++;
// jsonList.add(node);
}
mobileNode.put("imgList",imgList);
jsonMobileList.add(mobileNode);
}
node.put("mobileList",jsonMobileList);
jsonList.add(node);
}