java - Using Jackson to build custom JSON with or without Marshaling? -


am building json string this:

public string buildautocompletequery(int from, int pagesize, string criteria) {     stringbuilder autocomplete = new stringbuilder();     autocomplete.append("{").append("\"from\"").append(":").append(from).append(",").append("\"size\"").append(":").append(pagesize).append(",").append("\"query\"").append(":").append("{").append("\"match\"").append(":").append("{")             .append("\"full_text\"").append(":").append("\"").append(criteria).append("\"").append("}").append("}}");     return autocomplete.tostring(); } 

after putting in sample parameters, when print (with pretty printing) looks this:

{     "from": 1,     "size": 15,     "query": {         "match": {             "full_text": "hello"         }     } } 

is there easier way create json object using jackson (with or without marshalling / databinding)?

one way should try can create class object , use jackson convert object string.

{ "from": 1, "size": 15, "query": {     "match": {         "full_text": "hello"     }   } } 

now create classes

public class autocomplete{    private integer from;    private integer size;    private query query;     // getters , setters }  public class query{   private match match; }  public class match{   private string full_text; } 

then use jackson convert autocomplete object, required json string.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -