Read HTTP-side JSON format files

Source: Internet
Author: User

1.servlet output JSON Format data:

A.map implementing a single-layer JSON file

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"); Solve Chinese garbled problem

PrintWriter out = Response.getwriter ();

Map map1 = new HashMap ();

Map1.put ("Name", "San, Zhang");
Map1.put ("Age", new Integer (22));
Map1.put ("Provinces", New String ("Shandong province");
Map1.put ("Citiy", New String ("Jinan"));
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 ("Shandong province");
Map2.put ("Citiy", New String ("Jinan"));
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. Direct output

Note Double quotes are changed to single quotes or escaped

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 Terminal access to 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;
/**
* Receive service-side JSON data
*/
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 ("raw Data:");
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);
}}

Read HTTP-side JSON format files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.