【问题标题】:How do i print json data using JSTL如何使用 JSTL 打印 json 数据
【发布时间】:2019-12-14 14:07:07
【问题描述】:

我想使用 JSTL 标签显示 JSON。
但发生错误
如何打印 JsonData1、JsonData2?

错误信息

javax.el.PropertyNotFoundException:在 java.util.HashMap$Node 类型上找不到属性“名称” javax.el.PropertyNotFoundException:在类型 java.util.HashMap$Node 上找不到属性“评级” javax.el.PropertyNotFoundException:在 java.util.HashMap$Node 类型上找不到属性“评论”

jsonData1

{
  "formatted_address": "369 Sang-doro, Sangdo-dong, Dongjak-gu, Seoul, South Korea",
  "name": "Soongsil University",
  "rating": 4.5,
  "geometry": {
    "viewport": {
      "southwest": {
        "lng": 126.9561097701073,
        "lat": 37.49496127010728
      },
      "northeast": {
        "lng": 126.9588094298927,
        "lat": 37.49766092989272
      }
    },
    "location": {
      "lng": 126.9574596,
      "lat": 37.4963111
    }
  },
  "photos": [
    {
      "photo_reference": "CmRaAAAAyUDBODCqYW_EVYKccDbRt9oXRAsTwpQOVsqsQfGnF8ECPUV_iMrhQnqSvewvKVSPYyJ9Z4NzmhnxZAuLaUVrG349fvz8hOIByjDQwMyoJ_fjkuZKRRZgVny3_RE-dV_9EhAk2TP_bhnDDDzviwB73HWUGhRIfzevRbEdJhBJu-i86iYUFgOe1g",
      "width": 3120,
      "html_attributions": [
        "<a href=\"https://maps.google.com/maps/contrib/114600581232781423634\">박영식</a>"
      ],
      "height": 4160
    }
  ],
  "place_id": "ChIJzUGSjdahfDURxZ5P_MX-EdM"
}

jsonData2

{
  "reviews": [
    {
      "author_name": "Benjamin Reim",
      "profile_photo_url": "https://lh6.ggpht.com/-KuFCnWa56yY/AAAAAAAAAAI/AAAAAAAAAAA/624c90s3fuk/s128-c0x00000000-cc-rp-mo-ba3/photo.jpg",
      "author_url": "https://www.google.com/maps/contrib/110076368758079993748/reviews",
      "rating": 5,
      "language": "en",
      "text": "Awesome university. Great variety of student clubs, nice atmosphere among the students, modern facilities and a very beautiful campus site.",
      "time": 1477717288,
      "relative_time_description": "3 years ago"
    },
    {
      "author_name": "Kim Jiho",
      "profile_photo_url": "https://lh6.ggpht.com/-gF8ueHov1fI/AAAAAAAAAAI/AAAAAAAAAAA/PpaMqjyEmtI/s128-c0x00000000-cc-rp-mo/photo.jpg",
      "author_url": "https://www.google.com/maps/contrib/111735439867054263549/reviews",
      "rating": 5,
      "language": "en",
      "text": "Here is best places for training parkour. ",
      "time": 1360156533,
      "relative_time_description": "6 years ago"
    },
    {
      "author_name": "Dajun Ding",
      "profile_photo_url": "https://lh5.ggpht.com/-JiebyN0jymE/AAAAAAAAAAI/AAAAAAAAAAA/Thp1FIdOh6g/s128-c0x00000000-cc-rp-mo/photo.jpg",
      "author_url": "https://www.google.com/maps/contrib/113953052797905419575/reviews",
      "rating": 5,
      "language": "en",
      "text": "The academic environment on campus is admirable.",
      "time": 1364281678,
      "relative_time_description": "6 years ago"
    },
    {
      "author_name": "Gatherlight Young",
      "profile_photo_url": "https://lh4.ggpht.com/-yNs4iJb-Jz4/AAAAAAAAAAI/AAAAAAAAAAA/doyiFBNF6g8/s128-c0x00000000-cc-rp-mo/photo.jpg",
      "author_url": "https://www.google.com/maps/contrib/114484921735380034491/reviews",
      "rating": 5,
      "language": "en",
      "text": "Nice University  95",
      "time": 1480327371,
      "relative_time_description": "3 years ago"
    },
    {
      "author_name": "Young Ho Kim",
      "profile_photo_url": "https://lh3.ggpht.com/-tz82Qksuyt4/AAAAAAAAAAI/AAAAAAAAAAA/RggaENmSQn0/s128-c0x00000000-cc-rp-mo-ba4/photo.jpg",
      "author_url": "https://www.google.com/maps/contrib/114527872121517025812/reviews",
      "rating": 4,
      "language": "en",
      "text": "Good buildings ",
      "time": 1500330691,
      "relative_time_description": "2 years ago"
    }
  ],
  "name": "Soongsil University",
  "rating": 4.5,
  "formatted_phone_number": "02-820-0114"
}

JSP 文件的正文

<body>
    <c:forEach items="${jsonData1}" var="list">
        ${list.name}
        ${list.rating}
    </c:forEach>

    <c:forEach items="${jsonData2}" var="list">
        ${list.reviews}
    </c:forEach>
</body>

【问题讨论】:

  • 我要显示 jsonData1 的名称、评分和 jsonData2 的评论

标签: json jsp jstl


【解决方案1】:

jsonData1 似乎是HashMap

您不想要forEach:为什么要迭代地图的条目?您想要访问与 HashMap 中的键关联的值。 Java 代码jsonData1.get("name") 的等价物也是如此。因此,使用 JSP EL:

${jsonData1['name']}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    相关资源
    最近更新 更多