import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.SocketTimeoutException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import org.json.JSONException; import org.json.JSONObject; /** * 根据网址,返回编码方式为utf-8的JSONObject对象 * 注:只适合请求响应为json格式网址 * @param src 来源网址 * @author chitianxiang $22th March, 2012 - 2:44 p.m */ static private JSONObject getJsonObj4Utf8(String src) throws Exception { return getJsonObj(src, "utf-8"); } /** * 根据网址,返回JSONObject对象 * 注:只适合请求响应为json格式网址 * @param src 来源网址 * @param code 编码方式 * @author chitianxiang $22th March, 2012 - 2:42 p.m */ static private JSONObject getJsonObj(String src, String code) { InputStreamReader reader = null; BufferedReader in = null; try { URL url = new URL(src); URLConnection connection = url.openConnection(); connection.setConnectTimeout(1000); reader = new InputStreamReader(connection.getInputStream(), code); in = new BufferedReader(reader); String line = null; //每行内容 int lineFlag = 0; //标记: 判断有没有数据 StringBuffer content = new StringBuffer(); while ((line = in.readLine()) != null) { content.append(line); lineFlag++; } return lineFlag == 0 ? null : new JSONObject(content.toString()); } catch (SocketTimeoutException e) { System.out.println("连接超时!!!"); return null; } catch (JSONException e) { System.out.println("网站响应不是json格式,无法转化成JSONObject!!!"); return null; } catch (Exception e) { System.out.println("连接网址不对或读取流出现异常!!!"); return null; } finally { if (in != null) { try { in.close(); } catch (IOException e) { System.out.println("关闭流出现异常!!!"); } } if (reader != null) { try { reader.close(); } catch (IOException e) { System.out.println("关闭流出现异常!!!"); } } } } //简单使用 //小秘书网菜单http://www.xiaomishu.com/shop/postajax/GetDishList.aspx?pageSize=12&typeId=t1&typeSort=A0&resId=C47E19P46723&version=2 //请求ajax网址 StringBuilder sbSrc = new StringBuilder(); sbSrc.append("http://www.xiaomishu.com/shop/postajax/GetDishList.aspx?pageSize=10"); sbSrc.append("&typeId=" + dataId); sbSrc.append("&typeSort=A0"); String resId = delEndSeparator(sourceUrl, "/"); sbSrc.append("&resId=" + resId.substring(resId.lastIndexOf("/"))); sbSrc.append("&version=2"); //转换成json对象取出菜单 JSONObject jo = getJsonObj4Utf8(sbSrc.toString()); if (jo == null) { continue; } JSONObject menu = jo.getJSONObject("items"); Iterator<String> keys = menu.keys(); while (keys.hasNext()) { String key = keys.next(); JSONObject goods= menu.getJSONObject(key); String title = goods.get("title").toString(); String price = goods.get("price").toString(); String[] goodsInfo = new String[] {title, price}; goodsInfoLst.add(goodsInfo); } goodsInfoMap.put(goodstype, goodsInfoLst); /** * 删除符合规则的结束符 * @param obj 等删除对象 * @param endWith 结束符 * @author chitianxiang $Feb 4th, 2012 */ static private String delEndSeparator(Object obj, String endWith) { String tempStr = ""; if (!(obj instanceof String)) { tempStr = obj.toString(); } else { tempStr = (String)obj; } return tempStr.endsWith(endWith) ? tempStr.substring(0, tempStr.length() - 1) : tempStr; }
详细解决方案
依据网址,返回JSONObject对象
热度:266 发布时间:2012-06-27 14:20:09.0
相关解决方案
- net.sf.json.JSONObject cannot be resolved解决办法
- ajax JSONObject.fromObject 执行的时候没有任何反映,该怎么处理
- [转载]JSONObject.fromObject(地图)(JSON与JAVA数据的转换)
- JSONObject 解析json的步骤
- jsonObject 转化应用
- jsonObject 转化 bean list 地图 array
- 关于json的操作 jsonArray-jsonObject
- new JSONObject()不执行 不报错 解决办法
- Extjs4-json处理日期的有关问题,JSONObject.toBean转换日期时显示当前日期
- 运用json-lib JSONObject JSONArray
- Json委以包 JSONObject
- JSONObject put accumulate element 步骤区别
- JSONObject 与 JSONArray 的运用
- JSONObject 郁闷的有关问题
- JSON格式的String 如何转成 net.sf.json.JSONObject
- java.lang.reflect.InvocationTargetException异常,JSONObject.fromObject(obj)时报错,
- JSONObject.toBean怎么转化带有Timestamp类型的对象
- JSONObject json = new JSONObject()异常
- 有没有人用过org.json.JSONObject?解决思路
- Java中json数据有关问题.老报错 A JSONObject text must begin with '{'
- JSONObject 的有关问题
- JSONObject,该怎么解决
- java jsonobject 的有关问题
- Java 线程中回到的 JsonObject 对象为Null
- JSONObject cannot be converted to JSONArray
- JSONObject 总是返回“空”:false
- 如何将字符串转换为 JSONObject?
- json对象转换为实体对象案例:JSONObject,JSONArray之alibaba.fastjson和net.sf.json的不同用法
- JSON、JSONObject 与 JSONArray 简单讲解
- java.lang.String cannot be cast to com.alibaba.fastjson.JSONObject