如何使用com.google.gson类创建json对象 [英] How to create json object using com.google.gson class

查看:374
本文介绍了如何使用com.google.gson类创建json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过传递另一个jsonObject来将jsonArray与jsonObject进行转换.请提供任何示例代码?

I'm trying to convert jsonArray with jsonObject by passing another jsonObject. Any sample code please?

{
"role": "CUSTOMER",
"operationId": "updateHomePlace",
"parameters": {
    "tcId": "f44015c8-d672-411b-a0f9-49cf9ef3f6b2",
    "otpVerification": "false",
    "password": "false",
    "homePlace": [{
        "address": "MIG 528, KPHB 1st Phase,, Kukatpally Housing Board Colony, Kukatpally, Hyderabad, Telangana 500072, India",
        "lat": "17.4866943",
        "lng": "78.3994029"
    }]
}
}

推荐答案

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

Like this you can write POJO / Model class for the response

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("role")
@Expose
private String role;
@SerializedName("operationId")
@Expose
private String operationId;
@SerializedName("parameters")
@Expose
private 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 ----- ------------------------------

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

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class HomePlace {

@SerializedName("address")
@Expose
private String address;
@SerializedName("lat")
@Expose
private String lat;
@SerializedName("lng")
@Expose
private 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 ----- ------------------------------

-----------------------------------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")
@Expose
private String tcId;
@SerializedName("otpVerification")
@Expose
private String otpVerification;
@SerializedName("password")
@Expose
private String password;
@SerializedName("homePlace")
@Expose
private 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;
}

}

使用Gson将此json解析为自定义模型后

After this Parse json to custom model using Gson

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

这篇关于如何使用com.google.gson类创建json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆