How do I expose data in a JSON format through a web service using Java?

后端 未结 9 1885
攒了一身酷
攒了一身酷 2021-01-30 14:23

Is there an easy way to return data to web service clients in JSON using java? I\'m fine with servlets, spring, etc.

9条回答
  •  感情败类
    2021-01-30 15:21

    Yup! Check out json-lib

    Here is a simplified code snippet from my own code that send a set of my domain objects:

    private String getJsonDocumenent(Object myObj) (
        String result = "oops";
        try {
            JSONArray jsonArray = JSONArray.fromObject(myObj);
    
            result = jsonArray.toString(2);  //indent = 2
    
        } catch (net.sf.json.JSONException je) {
    
            throw je;
        }
        return result;
    }
    

提交回复
热议问题