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
- JAVA基础一大堆0803JSON+网络连接+枚举
- JAVA基础一大堆0802拷贝文件流+网络连接
- JAVA基础一大堆0812通过Http协议访问网络
- JAVA基础一大堆0804线程
- JAVA基础一大堆0805线程
- JAVA基础一大堆0810数据库
- JAVA基础一大堆0811数据库
- JAVA基础一大堆0802集合+泛型
- JAVA基础一大堆0804XML+UDP
- JAVA基础一大堆0804反射+注解
- JAVA基础一大堆0810数据库小练习
- JAVA基础一大堆0813Web项目
- 枚举网络连接
- JAVA基础一大堆0801接口+匿名类+IO流
- java基础第七讲——集合、泛型、枚举、网络
- 【java基础】--Java枚举
- JSON 基础(来源网络)
- java基础---->枚举
- C# Chart控件使用心得
- MAVEN下实现spring boot打包成war包(spring boot 01)
- Java进阶(五十一)必须记住的Myeclipse快捷键
- 循环
- JSON解析
- JAVA基础一大堆0803JSON+网络连接+枚举
- SpringMvc ,如何将页面的List传入后台Controller
- 可用性评估方法
- 聊聊BFC吧
- 函数的定义与调用
- Web Service修炼之一XFire入门
- 在ubuntu下如何搜索文件?
- 15/8/2网络连接/C-S连接
- java 类加载机制