日期:2014-05-20 浏览次数:20894 次
public class JsonTest {
public static void main(String[] args) throws IOException, JSONException {
URL url=new URL("http://www.weather.com.cn/data/cityinfo/101010100.html");
InputStream is=url.openStream();
String js;
DataInputStream data=new DataInputStream(is);
js=data.readUTF();
JSONObject json=new JSONObject(js);
JSONObject array=json.getJSONObject("weatherinfo");
System.out.println("城市:"+array.getString("city")+"\t"+"最高温度:"+array.getString("temp2")+"\t"+"最低温度:"+array.getString("temp1"));
}
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import org.json.JSONObject;
public class Test04 {
public static void main(String[] args) throws IOException {
URL url=new URL("http://www.weather.com.cn/data/cityinfo/101010100.html");
InputStream is=url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String lines;
while ((lines = reader.readLine()) != null) {
sb.append(lines);
}
reader.close();
JSONObject json = new JSONObject(sb.toString());
JSONObject array=json.getJSONObject("weatherinfo");
System.out.println("城市:"+array.getString("city")+"\t"+"最高温度:"+array.getString("temp1")+"\t"+"最低温度:"+array.getString("temp2"));
}
}