java读取url中json文件中的json数据

  有时候需要远程从其他接口中获取json数据,如果遇到返回的json数据是一个文件而不直接是数据,那么可以通过以下方法进行读取:

    /**
     * 从数据接口获取到数据
     * @return
     * @throws Exception
     */
    public String readData() throws Exception {
        //创建StringBuffer类型的变量json,用于存放拼装好的json数据
        //StringBuffer json = new StringBuffer("");
        String json = "";
        //url中不可以出现空格,空格全部用%20替换
        String url = "http://192.168.1.190:8888/api/CityData/GetMonitorData?apikey=?&startTime=2017-03-12%2012:00:00&endTime=2017-03-14%2012:00:00&uCode=?&pCode=?,?,?,?,?&datatype=?&isSrc=?"; 
        URL urls = new URL(url);  
        java.net.HttpURLConnection conn = (java.net.HttpURLConnection)urls.openConnection();  
        //因为服务器的安全设置不接受Java程序作为客户端访问,解决方案是设置客户端的User Agent
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        conn.setDoOutput(true);  
     conn.setDoInput(true);
//只可以设置为GET方式,不可以使用POST方式 //conn.setRequestMethod("POST"); conn.setRequestMethod("GET");

      //设置编码格式为UTF-8
      conn.setRequestProperty("contentType", "UTF-8");

//得到输入流
        InputStream inputStream = conn.getInputStream(); 
        //从输入流中获取数据(一定要设置编码格式,不然在服务器端接收到的数据可能乱码)
        BufferedReader bf=new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
        String line=null;
        while((line=bf.readLine())!=null){//一行一行的读
            json = json + line;
        }
        if(inputStream!=null){  
            inputStream.close();  
        }
        String[] strs = json.split("\\\\");
        String str = "";
        StringBuffer jsons = new StringBuffer("");
        for (int i = 0; i < strs.length; i++) {
            str = strs[i];
            jsons = jsons.append(str);
        }
        jsons.replace(0, 1, "");
        jsons.replace(jsons.length()-1, jsons.length(), "");
        this.jsonObject = jsons.toString();
        return JSON;
    }

 

可以按照以下步骤来读取JSON文件数据并插入MySQL数据库: 1. 使用Java的FileReader类或者BufferedReader类读取JSON文件,并将其存储为字符串。 2. 使用JavaJSON库,比如Jackson或者Gson,将JSON字符串转换为Java对象。 3. 遍历Java对象,将其转换为SQL语句,并插入到MySQL数据库。 以下是一个示例代码,用于读取JSON文件并将其插入到MySQL数据库: ``` import java.io.BufferedReader; import java.io.FileReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import com.google.gson.Gson; public class InsertJSONtoMySQL { public static void main(String[] args) { String fileName = "data.json"; // JSON文件路径 String url = "jdbc:mysql://localhost:3306/test"; // MySQL连接地址 String user = "root"; // MySQL用户名 String password = "123456"; // MySQL密码 try { // 读取JSON文件 BufferedReader br = new BufferedReader(new FileReader(fileName)); String jsonStr = ""; String line; while ((line = br.readLine()) != null) { jsonStr += line; } br.close(); // 将JSON字符串转换为Java对象 Gson gson = new Gson(); Data data = gson.fromJson(jsonStr, Data.class); // 连接MySQL数据库 Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection(url, user, password); // 插入数据 String sql = "INSERT INTO table_name (field1, field2, field3) VALUES (?, ?, ?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, data.getField1()); ps.setInt(2, data.getField2()); ps.setDouble(3, data.getField3()); ps.executeUpdate(); // 关闭连接 ps.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } class Data { private String field1; private int field2; private double field3; public String getField1() { return field1; } public void setField1(String field1) { this.field1 = field1; } public int getField2() { return field2; } public void setField2(int field2) { this.field2 = field2; } public double getField3() { return field3; } public void setField3(double field3) { this.field3 = field3; } } ``` 其,Data类是根据JSON数据格式定义的Java类,用于存储从JSON文件读取数据。在插入数据时,需要将Data类的属性值设置为PreparedStatement的参数值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值