json文件的解析

1.用JSONObject类来解析json文件.
2.先导入依赖,具体如下:

	<dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>fastjson</artifactId>
       <version>1.2.15</version>
   </dependency> 

3.用一个json文件作为一个demo,我们来解析这个文件,名为RadarIF.json,具体格式如下.

{
"编号": "00001",
"位置": {
	"经度": "23.6778",
	"纬度": "50.6778",
	"高度": "100.00"
},
"点值": [{
		"值": "1000"
	},
	{
		"值": "1100"
	},
	{
		"值": "1200"
	}
		]
}

4.具体代码如下:

public class ReadJson {

public RadarIFMaterial getRadarIFMaterial(String file){
    RadarIFMaterial radarIFMaterial = new RadarIFMaterial();
    JSONObject data = JSONObject.parseObject(loadJson(file));
    if (data.containsKey("编号")){
        System.out.println("编号key值存在");
        System.out.print("编号:");
        String status = data.getString("编号");
        System.out.println(status);
        //往实体类里面set
        radarIFMaterial.setIntelligenceId(status);
    }else {
        System.out.println("key值不存在");
    }
    
    if (data.containsKey("位置")){
        System.out.println("位置key值存在");
        JSONObject data1 = JSONObject.parseObject(data.getString("位置"));
        if (data1.containsKey("经度")){
            System.out.print("经度key值存在:");
            System.out.println(data1.getDouble("经度"));
            radarIFMaterial.setStationLongitude(data1.getDouble("经度"));
        }else {
            System.out.println("key值不存在");
        }
        if (data1.containsKey("纬度")){
            System.out.print("纬度key值存在:");
            System.out.println(data1.getDouble("纬度"));
            radarIFMaterial.setStationLatitude(data1.getDouble("纬度"));
        }else {
            System.out.println("key值不存在");
        }
        if (data1.containsKey("高度")){
            System.out.print("高度key值存在:");
            System.out.println(data1.getDouble("高度"));
            radarIFMaterial.setStationAltitude_m(data1.getDouble("高度"));
        }else {
            System.out.println("key值不存在");
        }

    }else {
        System.out.println("key值不存在");
    }
  
    if (data.containsKey("点值")){
        System.out.print("点值key值存在");
        System.out.println(data.getJSONArray("点值"));

        JSONArray j=data.getJSONArray("点值");
        Integer[] arr = new Integer[j.size()];
        for (int i = 0; i <j.size() ; i++) {
            Object o=j.get(i);
            JSONObject js=(JSONObject)o;
            arr[i]=js.getInteger("值");
            System.out.println("值为:"+js.getInteger("值"));
        }
		//实体的对象是个数组
        radarIFMaterial.setFrequencyValue(arr);
    }else {
        System.out.println("key值不存在");
    }

    System.out.println(loadJson(file));
    return radarIFMaterial;
}

public  String loadJson (String file) {
    StringBuilder json = new StringBuilder();
    try {
        URL urlObject = new URL("file:///" +file);
        URLConnection uc = urlObject.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8"));
        String inputLine = null;
        while ( (inputLine = in.readLine()) != null) {
            json.append(inputLine);
        }
        in.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return json.toString();
}
}

5.ParseOver

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值