How correctly produce JSON by RESTful web service?

后端 未结 5 1138
无人共我
无人共我 2020-12-08 15:48

I am writing a web service the first time. I created a RESTful web service based on Jersey. And I want to produce JSON. What do I need to do to generate the

5条回答
  •  轮回少年
    2020-12-08 16:23

    You could use a package like org.json http://www.json.org/java/

    Because you will need to use JSONObjects more often.

    There you can easily create JSONObjects and put some values in it:

     JSONObject json = new JSONObject();
     JSONArray array=new JSONArray();
        array.put("1");
        array.put("2");
        json.put("friends", array);
    
        System.out.println(json.toString(2));
    
    
        {"friends": [
          "1",
          "2"
        ]}
    

    edit This has the advantage that you can build your responses in different layers and return them as an object

提交回复
热议问题