如何在java中使用restful webservices返回arraylist?

时间:2013-12-24 07:51:32

标签: java android arraylist

我想以json格式返回值。我正在使用(MediaType.APPLICATION_JSON)来返回值。如何使用此返回ArrayList? 例如:

[
  {
    "node_title": "Ambivalence About Government Will Be Topic at next Lecture ",
    "nid": "Topic - Get the Government Off of Our Backs – There Ought to Be a Law: Reconciling Our National Ambivalence About Government."
  },
  {
    "node_title": "Recycling initiative gains steam under new director",
    "nid": "University administrators listened and hired a sustainability coordinator whose main focus has been to heighten recycling efforts and awareness."
  },
  {
    "node_title": "Special Week to Combat Hate and Discrimination",
    "nid": "For the seventh year, University students will observe “Why Do You Hate Me?” Week, which will run from March 28th through April 2nd."
  },
  {
    "node_title": "AUSP joins Nursing School on mission trip to Caribbean",
    "nid": "The School of Audiology and Speech-Language Pathology during spring break went to Dominican Republic to provide much-needed assistance to a school for deaf and impoverished children."
  }
]

请指导我。

1 个答案:

答案 0 :(得分:2)

如果要将其解析为java代码,可以执行以下操作: 首先,获取Gson,谷歌库以使用JSON。

接下来,定义一个类:

class Node {
    String node_title;
    String nid;
}

然后你可以做

Type collectionType = new TypeToken<List<Node>>(){}.getType();
List<Node> details = gson.fromJson(myJsonString, collectionType);