java : mozilla rhino js JSON 天气预报_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java : mozilla rhino js JSON 天气预报

java : mozilla rhino js JSON 天气预报

 2018/9/1 15:47:30  belldeep  程序员俱乐部  我要评论(0)
  • 摘要:JSON.parse和JSON.stringify这两个浏览器自带(IE6/7除外)的方法平常我们经常用到,但是一般都只是用到了他们的第一个参数,比如:字符串转对象:JSON.parse('{}');对象转字符串:JSON.stringify({});https://mozilla.github.io/rhino/下载rhino1_7R5.zip,解压后运行cmdcdD:\rhino\rhino1_7R5java-jarjs.jarjson
  • 标签:Java JSON Mozilla JS
JSON.parse和JSON.stringify这两个浏览器自带(IE6/7除外)的方法平常我们经常用到,
但是一般都只是用到了他们的第一个参数,
比如: 字符串转对象:JSON.parse('{}');
      对象转字符串:JSON.stringify({});
https://mozilla.github.io/rhino/ 下载 rhino1_7R5.zip , 解压后运行 cmd
cd D:\rhino\rhino1_7R5
java -jar js.jar json.js
class="javascript">
// Import the Swing GUI components and a few other classes
var swingNames = new JavaImporter(javax.swing, javax.swing.event, javax.swing.border, java.awt,java.awt.event); 

importPackage(java.net);
importPackage(java.io);
importPackage(java.util);
importClass(java.lang.Thread);

with (swingNames) {
var frame = new JFrame("JSON 天气预报 "); 
//frame.setSize(600,400);
frame.setLocation(200,200);
var txtfield = new JTextField(40);               // URL entry field
var alist = ["北京","上海","天津","重庆","广州","杭州","武汉","长春"];
var blist = ["010100","020100","030100","040100","280101","210101","200101","060101"];
txtfield.setText("http://www.weather.com.cn/data/sk/101"+blist[4]+".html");
var button1 = new JButton("Go");            // Button to send 
var button2= new JButton("Clear"); 
var filechooser = new JFileChooser();            // A file selection dialog
var row = Box.createHorizontalBox();             // A box for field and button
var col = Box.createVerticalBox();               // For the row & progress bars
var padding = new EmptyBorder(3,3,3,3);          // Padding for rows
var texta = new JTextArea(10,30);
texta.setEditable(false);
texta.setLineWrap(true);
var sp = new JScrollPane(texta);
//sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

// Put them all together and display the GUIm
row.add(txtfield);                               // Input field goes in the row
row.add(button1);                                 // Button goes in the row
row.add(button2); 
col.add(row);                                    // Row goes in the column
col.add(sp);
frame.add(col);                                  // Column goes in the frame
row.setBorder(padding);                          // Add some padding to the row
frame.pack();                                    // Set to minimum size
frame.visible = true;                            // Make the window visible
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// KeyEvent: ENTER
txtfield.addActionListener(function() {
    try {
        var url = new URL(txtfield.text);
        new java.lang.Thread(function() { connect(url); }).start();
    } catch(e) {
        JOptionPane.showMessageDialog(frame, e.message, "Exception",
                                     JOptionPane.ERROR_MESSAGE);
    }
});
// When the user clicks the button, call this function
button1.addActionListener(function() {
    try {
        var url = new URL(txtfield.text);
        new java.lang.Thread(function() { connect(url);}).start();
    } catch(e) {
        JOptionPane.showMessageDialog(frame, e.message, "Exception",
                                     JOptionPane.ERROR_MESSAGE);
    }
});
// Clear
button2.addActionListener(function() {
    texta.setText("");
});
// 连接
function connect(url){
    try {
        var conn = url.openConnection(); // Get java.net.URLConnection
        conn.setConnectTimeout(5000);
        conn.connect();             // Connect and wait for headers
        var input = new InputStreamReader(conn.getInputStream(),"UTF-8");   
        var buf = new BufferedReader(input);
        var line = buf.readLine().toString();
        buf.close();
        input.close();
        var obj = JSON.parse(line.trim());
        var str ="";
        if (obj == null) str = line;
        else str = JSON.stringify(obj, null, 4); // 缩进4个空格
        texta.append(str+"\n");
    } catch(ex){
        texta.append(ex.message);
    }
}
}

参考: JSON.parse和JSON.stringify 参数详解
https://www.cnblogs.com/lmh2072005/p/5985431.html
上一篇: springmvc中实现拦截器 下一篇: 没有下一篇了!
发表评论
用户名: 匿名