springboot去讀json檔案解析json陣列處理

TonySQ發表於2020-03-14

package org.adv.fw.modular.adv.lot;

import org.adv.fw.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.adv.fw.core.common.constant.factory.PageFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import org.adv.fw.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import org.adv.fw.modular.adv.warpper.ProvinceWarpper;
import org.apache.coyote.http11.filters.VoidInputFilter;
import org.adv.fw.modular.adv.model.Area;
import org.adv.fw.modular.adv.model.City;
import org.adv.fw.modular.adv.model.Province;
import org.adv.fw.modular.adv.service.IAreaService;
import org.adv.fw.modular.adv.service.ICityService;
import org.adv.fw.modular.adv.service.IProvinceService;
import org.adv.fw.modular.adv.service.impl.CityServiceImpl;
import org.adv.fw.modular.adv.service.impl.ProvinceServiceImpl;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2019-04-10 13:56:41
 */
@Controller
@RequestMapping("/lot")
public class ProController extends BaseController {

    private String PREFIX = "/adv/province/";

    @Autowired
    private IProvinceService provinceService;
    
    @Autowired
    private ICityService CityService;
    @Autowired
    private IAreaService areaservice;
    
    @Value("classpath:city.json")
    private static Resource area;
    
    /**
     * 跳轉到首頁
     * @throws FileNotFoundException 
     */
    @RequestMapping("")
    public void index() throws FileNotFoundException {
        
        File file = ResourceUtils.getFile("classpath:city.json");
        String jsonData = jsonRead(file);
//        JSONObject jsonObject = JSONObject.parseObject(jsonData);
        JSONArray array = JSONArray.parseArray(jsonData);
        for(int i=0;i<array.size();i++){
            JSONObject jsonObject2 = array.getJSONObject(i);
            System.out.println("省id:"+array.getJSONObject(i).getString("areaId"));
            System.out.println("name:"+array.getJSONObject(i).getString("areaName"));
//            Province model=new Province();
//            model.setCode(array.getJSONObject(i).getString("areaId"));
//            model.setName(array.getJSONObject(i).getString("areaName"));
//            provinceService.insert(model);
            JSONArray city = JSONArray.parseArray(array.getJSONObject(i).getString("cities"));
            for(int c=0;c<city.size();c++)
            {
                System.out.println("cityid:"+city.getJSONObject(c).getString("areaId"));
                System.out.println("cityname:"+city.getJSONObject(c).getString("areaName"));
                 City city2=new City();
//                 city2.setCode(city.getJSONObject(c).getString("areaId"));
//                city2.setName(city.getJSONObject(c).getString("areaName"));
//                city2.setProvinceId(array.getJSONObject(i).getString("areaId"));
//                CityService.insert(city2);
                 JSONArray counties = JSONArray.parseArray(city.getJSONObject(c).getString("counties"));
                 for(int a=0;a<counties.size();a++)
                 {
                     Area m=new Area();
                     m.setCode(counties.getJSONObject(a).getString("areaId"));
                     m.setName(counties.getJSONObject(a).getString("areaName"));
                     m.setCityId(city.getJSONObject(c).getString("areaId"));
                     areaservice.insert(m);
                 }
                 
                 
            }
        }
    }
    
    private static String jsonRead(File file){
        Scanner scanner = null;
        StringBuilder buffer = new StringBuilder();
        try {
            scanner = new Scanner(file, "utf-8");
            while (scanner.hasNextLine()) {
                buffer.append(scanner.nextLine());
            }
        } catch (Exception e) {

        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
        return buffer.toString();
    }
  
}
————————————————
版權宣告:本文為CSDN博主「極北三少」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/maodoudou1217/article/details/89604000複製程式碼


相關文章