2012-09-09 36 views
0

有人可以帮助我解决一些关于如何使用android应用中的GSON谷歌库来建立我的Json字符串的方法。如何为WCF REST API生成提及的json格式

我已经使用JsonStringer生成了下面提到的字符串,如下所述。

JSONStringer stringer = new JSONStringer() 
        .object() 
        .key("loginRequest") 
        .object() 
        .key("UserId").value("007") 
        .key("Password").value("125987563") 
        .key("LicenseKey").value("My Project Key") 
        .key("MobileId").value("This is mobile id") 
        .endObject() 
        .endObject(); 

{"loginRequest":{"UserId":"007","Password":"125987563","LicenseKey":"This is License Key","MobileId":"This is mobile id"}} 

请指教,我怎样才能使用Gson生成上述字符串。

感谢

回答

0

这几乎是如出一辙:

final StringWriter sw = new StringWriter(); 
new JsonWriter(sw) 
.beginObject() 
.name("loginRequest") 
.beginObject() 
.name("userId").value("007") 
.name("password").value("123") 
.endObject() 
.endObject() 
.close();