首页 > 解决方案 > 如何解析 json(items : [{...}])

问题描述

信息.json

{
"lastBuildDate": "Mon, 16 Jul 2018 01:28:44 +0900",
"total": 2,
"start": 1,
"display": 2,
"items": [{
    "title": "<b>설빙</b> 경기광명철산점",
    "link": "http://sulbing.com/",
    "category": "카페,디저트&gt;빙수",
    "description": "디저트 카페, 빙수, 토스트, 커피, 스무디, 녹차라떼, 오미자차 등 판매.",
    "telephone": "02-2611-1478",
    "address": "경기도 광명시 철산동 410",
    "roadAddress": "경기도 광명시 오리로856번길 8-1",
    "mapx": "300065",
    "mapy": "542034"
}, {
    "title": "<b>설빙</b> 하안점",
    "link": "",
    "category": "카페,디저트&gt;빙수",
    "description": "경기도 광명시 하안동 위치, 디저트카페, 빙수 전문점.",
    "telephone": "02-899-0503",
    "address": "경기도 광명시 하안동 34-3",
    "roadAddress": "경기도 광명시 하안로 309 세인빌딩",
    "mapx": "301042",
    "mapy": "540690"
}]
}

我想解析这个 json 文件。

其中,我想解析 ' items : [{...}]' 的内容中的内容,但是教程并没有告诉我该怎么做。

我使用 Java,但 Kotlin 也可以(Android)

并且标签<b> <\b>会被gson删除吗?还是我应该删除它?如果是后者呢?

标签: jsonparsinggson

解决方案


我已经按照 json 结构构建了类:

class Information{
  public Date lastBuildDate;
  public int total;
  public int start;
  public int display;
  public List<Item> items;
}

class Item{
  public String title;
  public String link;
  public String category;
  public String description;
  public String telephone;
  public String address;
  public String roadAddress;
  public String mapx;
  public String mapy;
}

在简单的 pasre json 之后,通过以下方式进行对象:

new Gson().fromJson(json, Information.class)

我已将日期更改为2018-07-16原始 json。如果你想使用特殊的日期格式,你需要实现JsonSerializer<Date>.


推荐阅读