Java.util.Map to JSON Object with Jersey / JAXB / Jackson

前端 未结 4 981
遥遥无期
遥遥无期 2020-12-08 01:06

I\'ve been trying to create a Jersey REST Webservice. I want to receive and emit JSON objects from Java classes like the following:

@XmlRootElement
public cl         


        
4条回答
  •  渐次进展
    2020-12-08 02:04

    You can use google-gson. Here is a sample code:

        @Test
        public void testGson(){
           Book book = new Book();
           book.code = "1234";
           book.names = new HashMap();
           book.names.put("Manish", "Pandit");
           book.names.put("Some","Name");
           String json = new Gson().toJson(book);
           System.out.println(json);
       }
    

    The output is {"code":"1234","names":{"Some":"Name","Manish":"Pandit"}}

提交回复
热议问题