package com.hong.job_management;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonParse {
    public static byte[] readParse(String urlPath) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        URL url = new URL(urlPath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream inStream = conn.getInputStream();
        while ((len = inStream.read(data)) != -1) {
            outStream.write(data, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();//通过out.Stream.toByteArray获取到写的数据
    }

    public static List<String> getList(String urlPath) throws Exception {
        List<String> url_backgroud = new ArrayList<String>();
        byte[] data = readParse(urlPath);
        JSONArray array = new JSONArray(new String(data));
        for (int i = 0; i < array.length(); i++) {
            JSONObject item = array.getJSONObject(i);
            String icon = item.getString("icon");
            url_backgroud.add(icon);
        }
        return url_backgroud;
    }
}
