JSON数据格式
[{id:56,title:"xxxxx",timelength:90},{id:16,title:"xbbx",timelength:20}]
1 /** 2 * 获取最新的视频资讯 3 * @return 4 * @throws Exception 5 */ 6 public static List<News> getJSONLastNews() throws Exception{ 7 8 String path = "http://192.168.0.168:8080/web/ListServlet?format=json"; 9 URL url = new URL(path); 10 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 11 conn.setConnectTimeout(5000); 12 conn.setRequestMethod("GET"); 13 if(conn.getResponseCode() == 200){ 14 InputStream inStream = conn.getInputStream(); 15 return parseJSON(inStream); 16 } 17 return null; 18 } 19 /** 20 * 解析JSON数据 21 * @param inStream 22 * @return 23 */ 24 private static List<News> parseJSON(InputStream inStream) throws Exception{ 25 26 List<News> newses = new ArrayList<News>(); 27 byte[] data = StreamTool.read(inStream); 28 String json = new String(data); 29 JSONArray array = new JSONArray(json); 30 31 for(int i = 0 ; i < array.length() ; i++){ 32 33 JSONObject jsonObject = array.getJSONObject(i); 34 News news = new News(jsonObject.getInt("id"), jsonObject.getString("title"), jsonObject.getInt("timelength")); 35 newses.add(news); 36 } 37 return newses; 38 }
1 public class StreamTool { 2 /** 3 * 读取输入流数据 4 * @param inStream 5 * @return 6 */ 7 public static byte[] read(InputStream inStream) throws Exception{ 8 9 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 10 byte[] buffer = new byte[1024]; 11 int len = 0; 12 while( (len = inStream.read(buffer)) != -1 ){ 13 outStream.write(buffer, 0, len); 14 } 15 inStream.close(); 16 return outStream.toByteArray(); 17 } 18 19 }
Json生成
public static void main(String[] args)
{
try
{
String json = "{\"name\":\"reiz\"}";
JSONObject jsonObj = new JSONObject(json);
String name = jsonObj.getString("name");
jsonObj.put("initial", name.substring(0, 1).toUpperCase());
Map<String, String> ingredients = new HashMap<String, String>();
ingredients.put("apples", "3kg");
ingredients.put("sugar", "1kg");
ingredients.put("pastry", "2.4kg");
ingredients.put("bestEaten", "outdoors");
jsonObj.put("ingredients", ingredients);
System.out.println(jsonObj);
}
catch (JSONException e)
{
e.printStackTrace();
}
// {"ingredients":{"apples":"3kg","pastry":"2.4kg","bestEaten":"outdoors","sugar":"1kg"},
// "initial":"R",
// "name":"reiz"}
}
1 JSON示例 2 { 3 "name" : "微博开放平台", 4 "domain" : "openapi", 5 "geo_enabled" : true, 6 "followers_count" : 13247, 7 "statuses_count" : 158, 8 "favourites_count" : 0, 9 "city" : "8", 10 "description" : "新浪微博开放平台市场推广官方账号,如有技术问题,请@微博API或者发私信给微博API", 11 "verified" : true, 12 "status" : 13 { 14 "created_at" : "Mon Nov 29 16:08:43 +0800 2010", 15 "text" : "各位开发者,我们的论坛上线啦~http://sinaurl.cn/h4FWc7 欢迎大家的参与~另外,关于技术相关的问题,可以在论坛上提出,也可以@微博API 这个官方技术支持账号哦~感谢大家对开放平台的支持~[呵呵]", 16 "truncated" : false, 17 "in_reply_to_status_id" : "", 18 "in_reply_to_screen_name" : "", 19 "geo" : null, 20 "favorited" : false, 21 "in_reply_to_user_id" : "", 22 "id" : 3958728723, 23 "source" : "<a href=\"http://t.sina.com.cn\" rel=\"nofollow\">新浪微博</a>" 24 }, 25 "id" : 11051, 26 "gender" : "m", 27 "friends_count" : 5, 28 "screen_name" : "微博开放平台", 29 "allow_all_act_msg" : true, 30 31 } 32 33 34 35 JSONObject jsonObject = new JSONObject(json); 36 String name = jsonObject.getString("name"); 37 JSONObject statusObject =jsonObject.getJSONObject("status"); 38 String create = statusObject.getString("created_at");
浙公网安备 33010602011771号