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

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;
        }       
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值