java--json嵌套数组解析(2)

接上篇
controller中

@Controller
@RequestMapping("/moblieCategory")
public class Moblie_CategoryController {
@RequestMapping("/category")
public String mobileHomeCategory(Model model){
     String url="xxxx/mobile/category";
    String homeDataJsonArrayString=Moblie_Utils.loadJson(url);
    JSONObject jsonObject=null;
    jsonObject= JSON.parseObject(homeDataJsonArrayString);
    MobileCategory category = new MobileCategory();

    List<MobileBigCategory> mobileBigCategoryList=new ArrayList<MobileBigCategory>();
    ArrayList<MobileSmallCategory> mobileSmallCategoryList=null;
    MobileBigCategory bigCategory=null;
    MobileSmallCategory mobileSmallCategory=null;
   JSONArray dataArray=null;
    JSONArray childrenArray=null;
    dataArray=jsonObject.getJSONArray("data");

    for(int i=0;i<dataArray.size();i++){
        bigCategory=new MobileBigCategory();
        jsonObject=dataArray.getJSONObject(i);
        bigCategory.setId(Integer.parseInt(dataArray.getJSONObject(i).getString("id")));
        bigCategory.setName(dataArray.getJSONObject(i).getString("name");

        childrenArray=dataArray.getJSONObject(i).getJSONArray("children";
        if(childrenArray!=null && childrenArray.size()>0) {
            mobileSmallCategoryList = new ArrayList<MobileSmallCategory>();
            for (int j = 0; j < childrenArray.size(); j++) {
                mobileSmallCategory = new MobileSmallCategory();
                mobileSmallCategory.setId(Integer.parseInt(childrenArray.getJSONObject(j).getString("id")));
                mobileSmallCategory.setName(childrenArray.getJSONObject(j).getString("name"));
                mobileSmallCategory.setChildren(childrenArray.getJSONObject(j).getString("children"));
                mobileSmallCategory.setParentId(Integer.parseInt(childrenArray.getJSONObject(j).getString("parentId")));
                mobileSmallCategory.setImgUrl(childrenArray.getJSONObject(j).getString("image"));
                mobileSmallCategory.setBannerUrl(childrenArray.getJSONObject(j).getString("banner"));
                mobileSmallCategory.setType(childrenArray.getJSONObject(j).getString("type"));
                mobileSmallCategory.setFirstChar(childrenArray.getJSONObject(j).getString("first_char"));
                mobileSmallCategory.setEnabled(childrenArray.getJSONObject(j).getString("enabled"));
                mobileSmallCategoryList.add(mobileSmallCategory);
            }
        }
        bigCategory.setChildren(mobileSmallCategoryList);

        bigCategory.setParentId(Integer.parseInt(dataArray.getJSONObject(i).getString("parentId")));
        bigCategory.setImgUrl(dataArray.getJSONObject(i).getString("image"));
        bigCategory.setImgUrl(dataArray.getJSONObject(i).getString("banner"));
        bigCategory.setType(dataArray.getJSONObject(i).getString("type"));
        bigCategory.setFirstChar(dataArray.getJSONObject(i).getString("first_char"));
        bigCategory.setEnabled(dataArray.getJSONObject(i).getString("enabled"));

        mobileBigCategoryList.add(bigCategory);
    }
    model.addAttribute("mobileBigCategoryList",mobileBigCategoryList);
    return  "mobileCategory.category";//跳转的tiles(jsp)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值