本文整理匯總了Java中com.google.gson.Gson.toJson方法的典型用法代碼示例。如果您正苦於以下問題:Java Gson.toJson方法的具體用法?Java Gson.toJson怎麽用?Java Gson.toJson使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.gson.Gson
的用法示例。
在下文中一共展示了Gson.toJson方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: saveToFavoritesPOI
import com.google.gson.Gson; //導入方法依賴的package包/類
/****************************Methods used to operate one FAVORITE POIs******************************************/
public void saveToFavoritesPOI(ParcelablePOI poi) {
if (poi == null) return;
preferences = context.getSharedPreferences(FAVORITES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
ArrayList<ParcelablePOI> previouslyStored = getFavoritePOIs();
if (previouslyStored == null) {
//No previous data
previouslyStored = new ArrayList<>();
}
previouslyStored.add(0, poi);//add always new POIs to first position
//Store FAVORITES
Gson gson = new Gson();
String json = gson.toJson(previouslyStored);
editor.putString("FAVORITES", json);
editor.apply();
}
示例2: parseSet
import com.google.gson.Gson; //導入方法依賴的package包/類
private static <T, K> T parseSet(String json, Class<K> keyClass) {
Set<K> resultSet = new HashSet<>();
if (keyClass == null) {
return (T) resultSet;
}
Gson gson = getGson();
Set<K> set = gson.fromJson(json, new TypeToken<Set<K>>() {
}.getType());
for (K value : set) {
String valueJson = gson.toJson(value);
resultSet.add(gson.fromJson(valueJson, keyClass));
}
return (T) resultSet;
}
示例3: getImgAmounData
import com.google.gson.Gson; //導入方法依賴的package包/類
/**
*
* @param response
* @param request
* @throws IOException
*/
@RequestMapping(value = "/admin/getimgamountdata", method = RequestMethod.POST)
public void getImgAmounData(HttpServletResponse response, HttpServletRequest request) throws IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8"); // 設置響應responce編碼為utf-8
PrintWriter out = response.getWriter();
String json;
Gson gson = new Gson();
Assign assign = new Assign();
long amount10 = imageService.getAmountOfDefinedTask(10);
long amount20 = imageService.getAmountOfDefinedTask(20);
long amount30 = imageService.getAmountOfDefinedTask(30);
long all_task_amount = amount10 + amount20 + amount30;
assign.setAmount10(amount10 + "");
assign.setAmount20(amount20 + "");
assign.setAmount30(amount30 + "");
assign.setAll_task_amount(all_task_amount + "");
Message<Assign> message = new Message<Assign>("200", "", assign);// 初始化message對象
json = gson.toJson(message, Message.class); // 使用gson將Message對象轉化為字符串
out.print(json);
}
示例4: executeSPARQLSelect
import com.google.gson.Gson; //導入方法依賴的package包/類
/**
* Returns from a given concept the data properties and obejctproperties and
* to each objecproperty a concept in the case the step range is greater 1
*
* @param concept
* @param step
* range
* @param
* @return
*/
@CrossOrigin
@RequestMapping(value = "/executeSPARQLSelect", method = RequestMethod.GET)
HttpEntity<Object> executeSPARQLSelect(@RequestParam("inputAsJson") String inputAsJson) {
Logger.getAnonymousLogger().log(Level.INFO, "Invoke: executeSPARQLSelect: " + inputAsJson);
OutputForExecuteSelect outputForExecuteSelect = new OutputForExecuteSelect();
try {
Gson gson = new Gson();
InputParamaterForExecuteSelect inputParamaterForExecuteSelect = gson.fromJson(inputAsJson,
InputParamaterForExecuteSelect.class);
outputForExecuteSelect = sparqlDerivation.createSPARQLAndExecuteIT(inputParamaterForExecuteSelect);
String result = "";
result = gson.toJson(outputForExecuteSelect);
return new ResponseEntity<Object>(result, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<Object>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
示例5: onDestroy
import com.google.gson.Gson; //導入方法依賴的package包/類
@Override
public void onDestroy() {
super.onDestroy();
mlocationClient.stop();
myOrientationListener.stop();
Log.d("gaolei", "RouteService----0nDestroy---------------");
Gson gson = new Gson();
String routeListStr = gson.toJson(routPointList);
Log.d("gaolei", "RouteService----routeListStr-------------" + routeListStr);
Bundle bundle = new Bundle();
bundle.putString("totalTime", totalTime + "");
bundle.putString("totalDistance", totalDistance + "");
bundle.putString("totalPrice", totalPrice + "");
bundle.putString("routePoints", routeListStr);
Intent intent = new Intent(this, RouteDetailActivity.class);
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
if (routPointList.size() > 2)
insertData(routeListStr);
Utils.releaseWakeLock();
stopForeground(true);
}
示例6: create_update_user
import com.google.gson.Gson; //導入方法依賴的package包/類
public String create_update_user(Object data) {
Gson gson = new Gson();
String json = gson.toJson(data);
return post("user/createdituser", json);
}
示例7: getUploadZipListByUserList
import com.google.gson.Gson; //導入方法依賴的package包/類
/**
* 管理員獲取誌願者上傳的貢獻壓縮包
*
* @param start
* @param page_num
* @param response
* @param request
* @throws IOException
*/
@RequestMapping(value = "/admin/getcontributeuploadziplist", method = RequestMethod.POST)
public void getUploadZipListByUserList(@RequestParam("start") String start,
@RequestParam("page_num") String page_num, HttpServletResponse response, HttpServletRequest request)
throws IOException {
// 設置編碼格式
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
String json = "";
Gson gson = new Gson();
PrintWriter out = response.getWriter();
SplitObject<ContributeImgListOfAll> splitObject = new SplitObject<ContributeImgListOfAll>();
List<ContributeImgListOfAll> cImgs = new ArrayList<ContributeImgListOfAll>();
// 分頁查詢誌願者上傳貢獻的壓縮包列表
cImgs = contributeService.getContributeOfAllByUser(new Integer(start).intValue(),
new Integer(page_num).intValue());
splitObject.setList(cImgs);
// 計算數據的頁數
int num = contributeService.getAmountContributeOfAllByUser();
if (num % Constant.DEVI_NUM == 0) {
splitObject.setPages_num(num / Constant.DEVI_NUM);
} else {
splitObject.setPages_num(num / Constant.DEVI_NUM + 1);
}
// 查詢有記錄
Message<SplitObject<ContributeImgListOfAll>> message = new Message<SplitObject<ContributeImgListOfAll>>("200",
"", splitObject);
json = gson.toJson(message);
out.print(json);
}
示例8: setHyperTrackLiveUser
import com.google.gson.Gson; //導入方法依賴的package包/類
public static void setHyperTrackLiveUser(Context context, HyperTrackLiveUser user) {
SharedPreferences.Editor editor = getEditor(context);
Gson gson = new Gson();
String userJSON = gson.toJson(user);
editor.putString(HYPERTRACK_LIVE_USER, userJSON);
editor.apply();
}
示例9: preview
import com.google.gson.Gson; //導入方法依賴的package包/類
/**
* 群發消息預覽
* @param accessToken
* @param message
* @param touser 公眾號的openid
* @param towxname 微信號
* @return
*/
public MassReturnCode preview(String accessToken, MassMessage message, String touser, String towxname) {
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
String msgText = gson.toJson(message);
@SuppressWarnings("unchecked")
Map<String, Object> msgMap = gson.fromJson(msgText, HashMap.class);
if(msgMap.get("filter")!=null){
msgMap.remove("filter");
}
if(msgMap.get("touser")!=null){
msgMap.remove("touser");
}
if(msgMap.get("send_ignore_reprint")!=null){
msgMap.remove("send_ignore_reprint");
}
if(towxname!=null){
msgMap.put("towxname", towxname);
}else if(touser!=null){
msgMap.put("touser", touser);
}else{
throw new IllegalArgumentException("群發消息預覽參數不符合要求,touser和towxname不能都為空。");
}
String url = WEIXIN_MASS_MESSAGE_PREVIEW_LINK + "?access_token=" + accessToken;
return WeixinInterfaceHelper.post(url, msgMap, MassReturnCode.class);
}
示例10: requestExtendedInfo
import com.google.gson.Gson; //導入方法依賴的package包/類
public void requestExtendedInfo(EnumSIPValues... values) throws IOException {
Socket socket = new Socket(this.address.getAddress(), this.address.getPort());
try {
socket.setSoTimeout(5000);
//Setup json:
List<String> toRequest = new ArrayList<String>();
for (EnumSIPValues value : values) {
toRequest.add(value.toString());
}
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
String endString = gson.toJson(toRequest);
PrintWriter os = new PrintWriter(socket.getOutputStream());
os.write(endString + "\n");
os.flush();
//Now wait for data:
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
String json = "";
while ((line = is.readLine()) != null) {
json += line;
}
Type myTypeMap = new TypeToken<Map<String,Object>>(){}.getType();
extendedValues = gson.fromJson(json, myTypeMap);
} finally {
socket.close();
}
}
示例11: makeB2BRequest
import com.google.gson.Gson; //導入方法依賴的package包/類
private HashMap<String, Object> makeB2BRequest(String product, Business recipient) {
HashMap<String, Object> body = new HashMap<>();
body.put("username", username);
body.put("productName", product);
//
Gson gson = new Gson();
String json = gson.toJson(recipient);
HashMap map = gson.fromJson(json, HashMap.class);
body.putAll(map);
return body;
}
示例12: setAccountOrigin
import com.google.gson.Gson; //導入方法依賴的package包/類
public void setAccountOrigin(AccountOrigin accountOrigin) {
if (accountOrigin != null) {
Gson gson = new Gson();
String json = gson.toJson(accountOrigin);
setAccountOrigin(json);
}
}
示例13: setDataList
import com.google.gson.Gson; //導入方法依賴的package包/類
/**
* 保存List
*
* @param key sp key值
* @param datalist list
* @param <T> item 類型
*/
public static <T> void setDataList(String key, List<T> datalist) {
if (null == datalist || datalist.size() <= 0)
return;
Gson gson = new Gson();
//轉換成json數據,再保存
String strJson = gson.toJson(datalist);
SpUtils.putString(AppUtils.getContext(), key, strJson);
}
示例14: toJson
import com.google.gson.Gson; //導入方法依賴的package包/類
public static String toJson(Object object) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
Gson gson = gsonBuilder.create();
return gson.toJson(object);
}
示例15: parse
import com.google.gson.Gson; //導入方法依賴的package包/類
public String parse(){
Gson gson = gsonInstance();
return gson.toJson( this );
}