欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java : mozilla rhino js JSON 天气预报 博客分类: javajavascript javajsonrhino 

程序员文章站 2024-03-14 08:30:22
...
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
// 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
相关标签: java json rhino