How to convert HTTP Request Body into JSON Object in Java

后端 未结 6 887
挽巷
挽巷 2020-12-10 04:45

I am trying find a Java lib/api that will allow me to turn the contents of a HTTP Request POST body into a JSON object.

Ideally I would like to use a Apache Sling li

相关标签:
6条回答
  • 2020-12-10 04:58

    Use Gson. With this you can create class with private variables which represent the data you want : for example.

    meta:{
           name:"Example"
           firstname:"Example2"
         }
    data:[
          {
            title:"ecaetra"
            description:"qwerty"
          }
          ...
         ]
    

    Json Object could be retrieve like this :

    public class RetrieveData {
    
          private Meta meta;
          private List<Data> data;
    
          public Meta getMeta(){
                return meta;
          }
    
          public List<Data> getData(){
                return data;
          }
    }
    
    public class Meta {
    
          private String name;
          private String firstname;
    
          public String getName(){
                return name;
          }
    
          public String getFirstName(){
                return firstname;
          }
    
    }
    
    public class Data {
    
          private String title;
          private String description;
    
          public String getTitle(){
                return title;
          }
    
          public String getDescription(){
                return description;
          }
    
    }
    

    And your instruction are simple. Content is the content of your Page, you can retrieve it with Asynctask.

     Object o = new Gson().fromJson(Content, RetrieveData.class);
     data = (RetrieveData)o;
     // Get Meta
        data.getName(); // Example
        data.getFirstName(); // Example2
     // Get Data
        data.get(0).getTitle(); // position 0 : ecaetra
        data.get(0).getDescription(); // position 0 : qwerty
    
    0 讨论(0)
  • 2020-12-10 05:01

    import org.json.JSONObject;

     JSONObject json = new JSONObject(request.getParameterMap())
    
    0 讨论(0)
  • 2020-12-10 05:02

    Jackson is also a good option - its used extensively in Spring. Here is the tutorial: http://wiki.fasterxml.com/JacksonInFiveMinutes

    0 讨论(0)
  • 2020-12-10 05:05

    Sorry on making this an own answer but obviously my reputation doesn't allow me to simply add a comment to the answer How to convert HTTP Request Body into JSON Object in Java of maerics.

    I would also iterate over the request params but instead of using an arbitrary json library use the JSONObject that is provided by sling. http://sling.apache.org/apidocs/sling6/org/apache/sling/commons/json/JSONObject.html

    0 讨论(0)
  • 2020-12-10 05:12

    I recommend trying Apache Commons Beanutils.

    ServeltRequest request;
    
    Map map = request.getParameterMap();
    MyObject object = new MyObject();
    BeanUtils.populate(object, map);
    String json = object.toJSON() //using any JSON library
    
    0 讨论(0)
  • 2020-12-10 05:13

    Assuming you're using an HttpServlet and a JSON library like json-simple you could do something like this:

    public JSONObject requestParamsToJSON(ServletRequest req) {
      JSONObject jsonObj = new JSONObject();
      Map<String,String[]> params = req.getParameterMap();
      for (Map.Entry<String,String[]> entry : params.entrySet()) {
        String v[] = entry.getValue();
        Object o = (v.length == 1) ? v[0] : v;
        jsonObj.put(entry.getKey(), o);
      }
      return jsonObj;
    }
    

    With example usage:

    public void doPost(HttpServletRequest req, HttpServletResponse res) {
      JSONObject jsonObj = requestParamsToJSON(req);
      // Now "jsonObj" is populated with the request parameters.
      // e.g. {"key1":"value1", "key2":["value2a", "value2b"], ...}
    }
    
    0 讨论(0)
提交回复
热议问题