Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

single key value to Json with Gson

I have single key-value pair that I need to convert to Json using Gson. How might I do that? Say I have

class MyClass{
  String key;
  String value;
  ...//bunch of other fields

  public String singleKeyValueToJson(){
    return new Gson(key,value);//how do I do this?
  }

}

Notice I am not serializing the whole class: just the two fields.

like image 634
Nouvel Travay Avatar asked Dec 08 '22 00:12

Nouvel Travay


1 Answers

why don't you create your json manually instead of using library.

public String singleKeyValueToJson(){
    JSONObject json=new JSONObject();
    json.put(key,value);
    return json.toString();
}
like image 161
Vivek Mishra Avatar answered Dec 11 '22 11:12

Vivek Mishra