2015-12-22 55 views
2

我有一個鍵值對,我需要使用Gson轉換爲Json。我該怎麼做?說我有JSON與Gson的單鍵值

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

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

} 

注意我沒有序列化整個類:只是兩個字段。

+0

你可以使用getter-setter方法 – Nisarg

+1

你爲什麼不手動創建的,而不是使用庫中的JSON JSONObject的JSON =新的JSONObject(); json.put(鍵,值); 返回JSON; –

+0

告訴我你的json – Rahul

回答

3

爲什麼不用手動創建你的json而不是使用庫。

JsonObject json=new JsonObject(); 
json.put(key,value); 
return json; 
0

您可以使用GSON象下面這樣:

public class MyClass { 

    private static final Gson gson = new Gson(); 

    String key; 
    String value; 

    public String singleKeyValueToJson() { 
     return gson.toJson(this); 
    } 

} 

注意GSON是靜態的,爲減少開銷,這是最終的,用於防止創建另一個GSON實例。

0
For gson u can write like this 


public class MyClass { 
    String key; 
    public MyClass(String value) { 
     this.key = value; 
    } 
    public String getKey() { 
     return key; 
    } 

    public void setKey(String value) { 
     this.key = value; 
    } 
} 


Gson gson = new Gson(); 

MyClass data = new MyClass("ValueData"); 

String requestString = gson.toJson(data); 
1

我使用谷歌GSON庫com.google.gson.Gson和代碼將是簡單而直接
當你只想以serailize的鍵值SimpleEntry是最好的班,你能做到這一點的,

public String singleKeyValueToJson() 
    { 
     Gson jsonObj = new Gson(); 
     SimpleEntry<String,String > keyValue = new SimpleEntry<String, String>(this.key, this.value); 
     return jsonObj.toJson(keyValue); 
    } 

編輯
你可以,如果你使用Gson對象的單個實例使內存利用率提高經常或多次序列化。

1

考慮具有3個變量,並返回唯一的2個變量的JSONObject的方法類,就像如下

class MyClass{ 
    String _value1 = "1234"; 
    int _value2 = 9100; 
    String _value3 = "5678"; 

    public String singleKeyValueToJson(){ 
     //add only selected variables to holder object(here HashMap), 
     HashMap<String,Object> map = new HashMap(); 
     map.put("key1",_value1); 
     map.put("key2",_value2); 
     //convert holder object to JSONObject directly and return as string as follows 
     return new Gson().toJson(map); 
    } 
} 

永遠不要忘記GSON LIB添加到您的項目,當你調用該方法返回的JSONObject的字符串像

{"key2":9100,"key1":"1234"} 
0
class A { 
public String key; 
public String value; 

public String singleKeyValueToJson() { 
     A als = new A(); 
     als.key= "text1"; 
     als.value= "text2"; 
     GsonBuilder builder = new GsonBuilder(); 
     Gson gson = builder.create(); 
     return gson.toJson(als);  
    } 
} 

,它會返回一個鍵值爲JSON與GSON

0

只是試圖手動創建json數據。 getJsonData()此方法將返回json。

import org.json.JSONException; 
import org.json.JSONObject;  

class MyClass{ 
    private String key; 
    private String value; 

    public String getkey() { 
      return key; 
    } 

    public void setkey(String key) { 
      this.key = key; 
    }  

    public String getValue() { 
      return value; 
    } 

    public void setValue(String value) { 
      this.value = value ; 
    } 


    public JSONObject getJsonData(){ 

    JSONObject jsonObject = new JSONObject(); 

    try { 
     jsonObject.put(getKey(), getValue()); 

    } catch (JSONException e) { 
      e.printStackTrace(); 
    } 
    return jsonObject; 

    }  
} 
2

你可以使用@Expose註釋來做到這一點。

import com.google.gson.annotations.Expose;

公共類圖書{

@Expose 
private String author; 
@Expose 
private String title; 
private Integer year; 
private Double price; 

public Book() { 
    this("test.org", "Exclude properties with Gson", 1989, 49.55); 
} 

public Book(String author, String title, Integer year, Double price) { 
    this.author = author; 
    this.title = title; 
    this.year = year; 
    this.price = price; 
} 

}

進口com.google.gson.Gson; import com.google.gson.GsonBuilder;

公共類WriteGson {

public static void main(String[] args) { 

    Gson gson = new GsonBuilder() 
      .excludeFieldsWithoutExposeAnnotation() 
      .create(); 

    String json = gson.toJson(new Book()); 
    System.out.println(json); 

    Gson gsonNonExcluded = new Gson(); 
    String jsonNonExcluded = gsonNonExcluded.toJson(new Book()); 
    System.out.println(jsonNonExcluded); 
} 

}

{ 「作者」:「測試。「」作者「:」test.org「,」標題「:」與Gson排除屬性「,」年份「:1989,」價格「:49.55}