1. 程式人生 > >讀取json數組裡面的某個資料

讀取json數組裡面的某個資料

程式碼
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Weather {
static JSONObject jsonobject,jsonobject1;
static JSONArray jsonArray;
public static void main(String[] args) throws IOException {
URL u = new URL(“

http://www.weather.com.cn/data/sk/101110101.html”);
InputStream in = u.openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
byte buf[] = new byte[1024];
int read = 0;
while ((read = in .read(buf)) > 0) {
out.write(buf, 0, read);
}
} finally {
if ( in != null) {
in .close();
}
}
byte b[] = out.toByteArray();
String result=new String(b, “utf-8”);
System.out.println(result);
jsonobject = JSONObject.fromObject(result);
System.out.println(jsonobject.getString(“weatherinfo”));
String data=jsonobject.getString(“weatherinfo”);
jsonobject1 = JSONObject.fromObject(data);
System.out.println( jsonobject1.getString(“temp”));
}

}

結果在這裡插入圖片描述