获取解析JSON数据


public class GetJson {

    private int WKID; // 坐标系
    private int DPI;// 图片分辨率
    private Point originPoint;// 圆点坐标
    private int rows;// 瓦片高度
    private int cols;// 瓦片宽度
    private double[] RESOLUTION;// 分辨率数组
    private double[] SCALE;// 比列尺数组
    private Envelope INIEIALEXTENT;// 初始化显示范围
    private Envelope FULLEXTENT;// 最大显示范围
    private String jsonUrl;

    public int getWKID() {
        return WKID;
    }

    public int getDPI() {
        return DPI;
    }

    public Point getOriginPoint() {
        return originPoint;
    }

    public int getRows() {
        return rows;
    }

    public int getCols() {
        return cols;
    }

    public double[] getRESOLUTION() {
        return RESOLUTION;
    }

    public double[] getSCALE() {
        return SCALE;
    }

    public Envelope getINIEIALEXTENT() {
        return INIEIALEXTENT;
    }

    public Envelope getFULLEXTENT() {
        return FULLEXTENT;
    }

    public GetJson(String url) {
        // TODO Auto-generated constructor stub
        this.jsonUrl = url;
        getJsonStr(url);
    }

    public void getJsonStr(String url) {
        HttpGet httpGet = new HttpGet(url);
        HttpClient httpClient = new DefaultHttpClient();
        StringBuilder builder = new StringBuilder();
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {

                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(httpEntity.getContent()));
                String str;
                for (str = bufferedReader.readLine(); str != null; str = bufferedReader
                        .readLine()) {
                    builder.append(str);

                }
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            // 获取瓦片信息
            JSONObject jsonObject = new JSONObject(builder.toString())
                    .getJSONObject("tileInfo");
            rows = jsonObject.getInt("rows");
            cols = jsonObject.getInt("cols");
            DPI = jsonObject.getInt("dpi");

            // 获取圆点坐标
            JSONObject jsonObject1 = new JSONObject(builder.toString())
                    .getJSONObject("tileInfo").getJSONObject("origin");
            Double x = jsonObject1.getDouble("x");
            Double y = jsonObject1.getDouble("y");
            originPoint = new Point(x, y);

            // 获取显示范围
            JSONObject jsonObject2 = new JSONObject(builder.toString())
                    .getJSONObject("initialExtent");
            Double xmin = jsonObject2.getDouble("xmin");
            Double ymin = jsonObject2.getDouble("ymin");
            Double xmax = jsonObject2.getDouble("xmax");
            Double ymax = jsonObject2.getDouble("ymax");
            INIEIALEXTENT = new Envelope(xmin, ymin, xmax, ymax);

            JSONObject jsonObject3 = new JSONObject(builder.toString())
                    .getJSONObject("fullExtent");
            Double Xmin = jsonObject3.getDouble("xmin");
            Double Ymin = jsonObject3.getDouble("ymin");
            Double Xmax = jsonObject3.getDouble("xmax");
            Double Ymax = jsonObject3.getDouble("ymax");
            FULLEXTENT = new Envelope(Xmin, Ymin, Xmax, Ymax);

            // 获取坐标系
            JSONObject jsonObject4 = new JSONObject(builder.toString())
                    .getJSONObject("spatialReference");
            WKID = jsonObject4.getInt("wkid");

            // 获取分辨率和比例尺数组
            JSONArray jsonArray = jsonObject.getJSONArray("lods");
            int k = jsonArray.length();
            RESOLUTION = new double[k];
            SCALE = new double[k];
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jObject = (JSONObject)jsonArray.opt(i);
                RESOLUTION[i] = jObject.getDouble("resolution");
                SCALE[i] = jObject.getDouble("scale");
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            String msg = e.toString();
            Log.i("解析JSON数据异常:", msg);
        }

    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值