gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:23:55 25 4
gpt4 key购买 nike

我想返回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."
}
]

请指导我。

最佳答案

如果你想将其解析为java代码,你可以这样做:首先,获取Gson ,一个使用 JSON 的 Google 库。

接下来,定义一个类:

class Node {
String node_title;
String nid;
}

然后你就可以了

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

关于java - 如何在java中使用restful webservices返回arraylist?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20757016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com