本文整理汇总了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 );
}