2013-12-24 2 views
1

Я хочу вернуть значения в формате json. Я использую (MediaType.APPLICATION_JSON) для возврата значений. Как вернуть ArrayList с помощью этого? Пример:Как вернуть arraylist, используя restful webservices в java?

[ 
    { 
    "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." 
    } 
] 

Прослушайте меня.

+0

Является ли это JSON-то вы получаете откуда-то (как веб-службы), и вы хотите, чтобы разобрать в аррайалист? –

+1

Посмотрите http://www.jsonschema2pojo.org/ – user370305

ответ

2

Если вы хотите разобрать это на Java-код, вы можете сделать что-то вроде этого: Сначала получите Gson, библиотеку google для работы с JSON.

Далее, определим класс как:

class Node { 
    String node_title; 
    String nid; 
} 

Тогда вы можете сделать

Type collectionType = new TypeToken<List<Node>>(){}.getType(); 
List<Node> details = gson.fromJson(myJsonString, collectionType); 
Смежные вопросы