zoukankan      html  css  js  c++  java
  • 读取http端json格式文件

    1.servlet输出json格式数据:

    a.map实现单层json文件

    package JsonManager;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.HashMap;
    import java.util.Map;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import net.sf.json.JSONObject;

    public class JsonServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    this.doPost(request, response);
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8"); //解决中文乱码问题

    PrintWriter out = response.getWriter();

    Map map1 = new HashMap();

    map1.put("name", "san、zhang");
    map1.put("age", new Integer(22));
    map1.put("Provinces", new String("山东省"));
    map1.put("citiy", new String("济南市"));
    map1.put("Master", new String("C、C++、Linux、Java"));

    Map map2 = new HashMap();

    map2.put("name", "san、zhang");
    map2.put("age", new Integer(22));
    map2.put("Provinces", new String("山东省"));
    map2.put("citiy", new String("济南市"));
    map2.put("Master", new String("C、C++、Linux、Java"));
    JSONObject json1 = JSONObject.fromObject(map1);
    JSONObject json2 = JSONObject.fromObject(map2);

    out.write(json1.toString());
    out.write(json2.toString());
    //out.write(map.toString());
    out.flush();
    out.close();}

    b.直接输出

    注意双引号改为单引号或者用转义

    String arg0= "{‘dataset’:[{‘label’:[‘58.213.14.210’],"+
    "‘data’:[{‘time’:’2016-08-01 12:00:00+08’,‘value’:[‘240651744.00’]}," +
    "{‘time’:’2016-08-01 12:05:00+08’,‘value’:[‘230709888.00’]},"+
    "{‘time’:’2016-08-01 12:10:00+08’, ‘value’:[‘231472528.00’]}," +
    "{‘time’:’2016-08-01 12:15:00+08’,‘value’:[‘225603200.00’]},"+
    "{‘time’:’2016-08-01 12:20:00+08’,‘value’:[‘222865600.00’]},"+
    "{‘time’:’2016-08-01 12:25:00+08’,‘value’:[‘227648016.00’]},"+
    "{‘time’:’2016-08-01 12:30:00+08’,‘value’:[‘229146144.00’]},"+
    "{‘time’:’2016-08-01 12:35:00+08’,‘value’:[‘229744544.00’]},"+
    "{‘time’:’2016-08-01 12:40:00+08’,‘value’:[‘228680000.00’]},"+
    "{‘time’:’2016-08-01 12:45:00+08’,‘value’:[‘235564544.00’]},"+
    "{‘time’:’2016-08-01 12:50:00+08’,‘value’:[‘225956016.00’]},"+
    "{‘time’:’2016-08-01 12:55:00+08’,‘value’:[‘223865600.00’]},"+
    "{‘time’:’2016-08-01 13:00:00+08’,‘value’:[‘219243472.00’]}]},"+
    "{‘label’:[‘58.213.111.114’],"+
    "‘data’:[ {‘time’:’2016-08-01 12:00:00+08’,‘value’:[‘85914136.00’]},"+
    "{‘time’:’2016-08-01 12:05:00+08’,‘value’:[‘104741072.00’]},"+
    "{‘time’:’2016-08-01 12:10:00+08’,‘value’:[‘93126136.00’]},"+
    "{‘time’:’2016-08-01 12:15:00+08’,‘value’:[‘70651200.00’]},"+
    "{‘time’:’2016-08-01 12:20:00+08’,‘value’:[‘77301336.00’]},"+
    "{‘time’:’2016-08-01 12:25:00+08’,‘value’:[‘87298136.00’]},"+
    "{‘time’:’2016-08-01 12:30:00+08’,‘value’:[‘109632000.00’]},"+
    "{‘time’:’2016-08-01 12:35:00+08’,‘value’:[‘119785600.00’]},"+
    "{‘time’:’2016-08-01 12:40:00+08’,‘value’:[‘87119736.00’]},"+
    "{‘time’:’2016-08-01 12:45:00+08’,‘value’:[‘128524264.00’]},"+
    "{‘time’:’2016-08-01 12:50:00+08’,‘value’:[‘104490936.00’]},"+
    "{‘time’:’2016-08-01 12:55:00+08’,‘value’:[‘88409072.00’]},"+
    "{‘time’:’2016-08-01 13:00:00+08’,‘value’:[‘80157072.00’]}]}]}}";
    out.write(arg0);

    2.java端接入http

    package JsonManager;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;

    import sun.net.www.protocol.http.HttpURLConnection;
    /**
    * 接收服务端Json数据
    */
    public class GetJsonInterfaceInfo{
    /**
    * @param args
    * @throws IOException
    */

    public void loadJSON (String start_time,String end_time) {
    String urlPath="http://localhost:8080/JSONInterface/servlet/jsonServlet"+
    "?request={'report_request':{'resource_type':'filter','report_type':'topn','resource_id':"+
    "'1397','counter_index':'1','granularity':'5min','report_datetime':"+
    "{'start_time':"+start_time+",'end_time':"+end_time+"},'topn_index':'3792'}}"
    ;

    StringBuilder json = new StringBuilder();
    try {
    URL url = new URL(urlPath);
    URLConnection con = url.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(
    con.getInputStream()));
    String inputLine = null;
    while ( (inputLine = in.readLine()) != null) {
    json.append(inputLine);
    }
    in.close();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
    System.out.println("原始数据:");
    System.out.println(json.toString());
    }

    public static void main(String[] args) {
    String start_time ="";
    String end_time ="";
    if(args.length >=1 && args[0] !=null){
    start_time = args[0];
    }
    if(args.length >=2 && args[1] !=null){
    start_time = args[1];
    }
    GetJsonInterfaceInfo GI=new GetJsonInterfaceInfo();
    GI.loadJSON(start_time,end_time);
    }}

  • 相关阅读:
    Laravel自动备份到阿里云OSS
    《Modern PHP》读书笔记
    支持IE6、IE7、IE8等低端浏览器的简化版vue
    利用SSH 反向代理 ,实现跨局域网连接家里的linux 主机 (树莓派)
    tensorflow-gpu安装脚本
    c++后台开发面试常见知识点总结(六)算法手写
    c++后台开发面试常见知识点总结(五)场景设计
    c++后台开发面试常见知识点总结(四)数据库
    c++后台开发面试常见知识点总结(三)操作系统
    c++后台开发面试常见知识点总结(二)网络编程
  • 原文地址:https://www.cnblogs.com/vicky-upc/p/5763573.html
Copyright © 2011-2022 走看看