JAVA基础一大堆0803JSON+网络连接+枚举

来源:互联网 发布:JAVA中的maven是什么 编辑:程序博客网 时间:2024/05/16 09:04

JSON

public class JSONTest {    public static void main(String[] args) {        String json=creatJSON();        JSONObject obj=JSONObject.fromObject(json);        System.out.println(obj.getString("city"));        JSONObject today=obj.getJSONObject("today");        JSONArray array=today.getJSONArray("index");        for(int i=0;i<array.size();i++){            JSONObject obj1=array.getJSONObject(i);            System.out.println(obj1.getString("name"));        }    }    private static String creatJSON(){        JSONObject obj=new JSONObject();        obj.put("city","北京");        obj.put("cityid", "123");        JSONObject today=new JSONObject();        today.put("date", "2015-08-03");        today.put("week", "星期一");        JSONArray array=new JSONArray();        JSONObject index1=new JSONObject();        index1.put("name", "感冒");        JSONObject index2=new JSONObject();        index2.put("name", "防晒");        JSONObject index3=new JSONObject();        index3.put("name", "炎热");        array.add(index1);        array.add(index2);        array.add(index3);        today.put("index", array);        obj.put("today", today);        System.out.println(obj.toString());        return obj.toString();    }}

运行结果
{
“city”: “北京”,
“cityid”: “123”,
“today”: {
“date”: “2015-08-03”,
“week”: “星期一”,
“index”: [
{
“name”: “感冒”
},
{
“name”: “防晒”
},
{
“name”: “炎热”
}
]
}
}
北京
感冒
防晒
炎热

网络连接

URL

import java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.MalformedURLException;import java.net.URL;public class UrlTest {    public static void main(String[] args) {        // 输出网页源码        try {            URL url1 = new URL("http://www.baidu.com");// 注意网址的斜线            InputStream is = url1.openStream();            BufferedReader br = new BufferedReader(new InputStreamReader(is));            String line = br.readLine();            while (line != null) {                System.out.println(line);                line = br.readLine();            }            br.close();            is.close();        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}

这里写图片描述

复制网页上的图片

import java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.MalformedURLException;import java.net.URL;public class UrlTest {    public static void main(String[] args) {        try {            URL url1=new URL("http://pic.nipic.com/2007-11-09/2007119122519868_2.jpg");            File img=new File("d:\\11.jpg");            if(!img.exists()){                img.createNewFile();            }            OutputStream os=new FileOutputStream(img);            InputStream is=url1.openStream();                                                                                                                                byte[] array=new byte[1024];            int i=is.read(array);            while(i!=-1){            os.write(array, 0, i);            i=is.read(array);            }            os.flush();            os.close();            is.close();        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}

这里写图片描述

枚举

public enum Sex {    FEMALE,MALE;    //相当于public static final int FEMALE=0X1;}public class Student {    private String name;    private Sex sex;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Sex getSex() {        return sex;    }    public void setSex(Sex sex) {        this.sex = sex;    }}public class Test {    public static void main(String[] args) {        Student zhangsan=new Student();        zhangsan.setSex(Sex.FEMALE);        switch (zhangsan.getSex()) {        case FEMALE:            break;        case MALE:            break;        default:            break;        }           }}
0 0
原创粉丝点击