java 解析Json对象(嵌套json数组)

野性酷女 2023-07-13 10:56 209阅读 0赞

在这里我需要读取的是json数据中promote的img的url,name,shopPrice。
1.实体类
20160908161534292

2.工具类Moblie_Utils根据url获取网络json格式数据

  1. public class Moblie_Utils {
  2. public static String loadJson(String url) {
  3. StringBuilder json = new StringBuilder();
  4. try {
  5. URL urlObject = new URL(url);
  6. URLConnection uc = urlObject.openConnection();
  7. BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
  8. String inputLine = null;
  9. while ((inputLine = in.readLine()) != null) {
  10. json.append(inputLine);
  11. }
  12. // System.out.println("json.toString()====="+json.toString());
  13. in.close();
  14. } catch (MalformedURLException e) {
  15. e.printStackTrace();
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }
  19. return json.toString();
  20. }}

3.json数据格式,json中包含嵌套json数组

  1. {
  2. "data": {
  3. "promote": [
  4. {
  5. "id": 24,
  6. "goods_id": 24,
  7. "brief": null,
  8. "img": {
  9. "thumb": "http://image/1_0.jpg",
  10. "url": "http://image/1_0.jpg",
  11. "small": "http://image/1_0.jpg"
  12. },
  13. "market_price": "0.0",
  14. "name": "F2000",
  15. "promote_price": "",
  16. "shop_price": "0.01"
  17. },
  18. {
  19. "id": 92,
  20. "goods_id": 92,
  21. "brief": null,
  22. "img": {
  23. "thumb": "http://image/1_0.jpg",
  24. "url": "http://image/1_0.jpg",
  25. "small": "http://image/1_0.jpg"
  26. },
  27. "market_price": "0.0",
  28. "name": "23000",
  29. "promote_price": "",
  30. "shop_price": "0.01"
  31. }
  32. ],
  33. "player": [
  34. {
  35. "action": null,
  36. "action_id": null,
  37. "description": null,
  38. "photo": {
  39. "thumb": null,
  40. "url": "http://image/1_0.jpg",
  41. "small": null
  42. },
  43. "url": ""
  44. },
  45. {
  46. "action": null,
  47. "action_id": null,
  48. "description": null,
  49. "photo": {
  50. "thumb": null,
  51. "url": "http://image/1_0.jpg",
  52. "small": null
  53. },
  54. "url": ""
  55. }
  56. ]
  57. },
  58. "status": {
  59. "succeed": 1,
  60. "error_code": null,
  61. "error_desc": null
  62. },
  63. "paginated": null,
  64. "session": null

}

4.控制层controller中

  1. @Controller
  2. @RequestMapping("/moblieHome")
  3. public class Moblie_IndexController {
  4. @RequestMapping(value = "/hotSale",produces = "text/html;charset=UTF-8")
  5. public String mobileHomeCategory(Model model){
  6. String url="http://xxxxx/home/data";
  7. String homeDataJsonArrayString=Moblie_Utils.loadJson(url);
  8. JSONObject jsonObject=null;
  9. jsonObject= JSON.parseObject(homeDataJsonArrayString);
  10. String data=jsonObject.getString("data");
  11. if(data!=null &&data.length()>0){
  12. System.out.println("data==="+data);
  13. }
  14. jsonObject= JSON.parseObject(data);
  15. String promote_goods=jsonObject.getString("promote_goods");
  16. if(data!=null &&data.length()>0){
  17. System.out.println("promote_goods==="+promote_goods);
  18. }
  19. JSONArray jsonArray =jsonObject.getJSONArray("promote_goods");
  20. JSONArray jsonArrayImg=null;
  21. JSONObject object=null;
  22. JSONObject objectImg=null;
  23. List<MobileGoodsBrief> goodsList = new ArrayList<MobileGoodsBrief>();
  24. MobileGoodsBrief good=null;
  25. if(jsonArray!=null && jsonArray.size()>0) {
  26. for (int i = 0; i < jsonArray.size(); i++) {
  27. object = jsonArray.getJSONObject(i);
  28. good=new MobileGoodsBrief();
  29. good.setShopPrice(object.getString("shop_price"));
  30. good.setPrice(object.getString("market_price"));
  31. good.setName(object.getString("name"));
  32. objectImg = JSON.parseObject(object.getString("img").toString());
  33. good.setImgUrl(objectImg.getString("url"));
  34. goodsList.add(good);
  35. }
  36. }
  37. model.addAttribute("goodsList",goodsList);
  38. return "index.jsp";
  39. }

5.controller传值给jsp,
jsp头部注意添加c标签:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="t" uri="http://tiles.apache.org/tags-tiles"%>
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. </head>
  8. <body>
  9. <div>
  10. <ul class="good">
  11. <c:forEach items="${goodsList}" var="t">
  12. <li>
  13. <a target="_blank" href="jump/67939165">
  14. <img src="${t.imgUrl}"/>
  15. </a>
  16. <a target="_blank" href="jump/67939165">
  17. <h1><i class="ico13"><img src="${t.imgUrl}"></i> ${t.name}</h1>
  18. <div class="list-price buy">
  19. <i></i><span class="price-new">${t.shopPrice}</span>
  20. <i class="del">/¥${t.price}</i>
  21. <span class="good-btn"><i class="ico15">
  22. <img src="images/sts.png"/>
  23. </i> 去抢购
  24. </span>
  25. </div>
  26. </a>
  27. </li>
  28. </c:forEach>
  29. </ul>
  30. </div>
  31. </body>
  32. </html>

发表评论

表情:
评论列表 (有 0 条评论,209人围观)

还没有评论,来说两句吧...

相关阅读