栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用com.google.gson类创建json对象

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用com.google.gson类创建json对象

这样,您可以为响应编写POJO / Model类

package com.example;import com.google.gson.annotations.Expose;import com.google.gson.annotations.SerializedName;public class Example {@SerializedName("role")@Exposeprivate String role;@SerializedName("operationId")@Exposeprivate String operationId;@SerializedName("parameters")@Exposeprivate Parameters parameters;public String getRole() {return role;}public void setRole(String role) {this.role = role;}public String getOperationId() {return operationId;}public void setOperationId(String operationId) {this.operationId = operationId;}public Parameters getParameters() {return parameters;}public void setParameters(Parameters parameters) {this.parameters = parameters;}}

----------------------------------- com.example.HomePlace.java --------

package com.example;import com.google.gson.annotations.Expose;import com.google.gson.annotations.SerializedName;public class HomePlace {@SerializedName("address")@Exposeprivate String address;@SerializedName("lat")@Exposeprivate String lat;@SerializedName("lng")@Exposeprivate String lng;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getLat() {return lat;}public void setLat(String lat) {this.lat = lat;}public String getLng() {return lng;}public void setLng(String lng) {this.lng = lng;}}

----------------------------------- com.example.Parameters.java --------

package com.example;import java.util.List;import com.google.gson.annotations.Expose;import com.google.gson.annotations.SerializedName;public class Parameters {@SerializedName("tcId")@Exposeprivate String tcId;@SerializedName("otpVerification")@Exposeprivate String otpVerification;@SerializedName("password")@Exposeprivate String password;@SerializedName("homePlace")@Exposeprivate List<HomePlace> homePlace = null;public String getTcId() {return tcId;}public void setTcId(String tcId) {this.tcId = tcId;}public String getOtpVerification() {return otpVerification;}public void setOtpVerification(String otpVerification) {this.otpVerification = otpVerification;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public List<HomePlace> getHomePlace() {return homePlace;}public void setHomePlace(List<HomePlace> homePlace) {this.homePlace = homePlace;}}

在将json解析为使用Gson的自定义模型之后

Gson gson = new GsonBuilder().create();Example yourModelClass = gson.fromJson(yourJsonResponse, Example .class);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410559.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号