简体   繁体   English

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

[英]How to create json object using com.google.gson class

I'm trying to convert jsonArray with jsonObject by passing another jsonObject. 我试图通过传递另一个jsonObject与jsonObject转换jsonArray。 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"
    }]
}
}

Like this you can write POJO / Model class for the response 这样,您可以为响应编写POJO / Model类

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;
}

}

After this Parse json to custom model using Gson 在将json解析为使用Gson的自定义模型之后

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

试试这个插件为android studio DTO Generator从给定的文本生成Java类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用com.google.gson进行数据输入 - Data entry using com.google.gson 如何在JsonArray(com.google.gson)中将Empty字符串转换为null - How to convert Empty string to null in JsonArray ( com.google.gson) 如何修复 Android Studio 中的“无法解析:com.google.gson:gson:2.8.5” - How to fix "Failed to resolve: com.google.gson:gson:2.8.5" in Android studio 无法使用 gson.toJson(Element) 获取 JSON 字符串,{模块 java.base 不会“打开 java.util”到模块 com.google.gson] - Can't get JSON String by using gson.toJson(Element), {module java.base does not "opens java.util" to module com.google.gson] 为什么JsonParser使用com.google.gson API在返回值中使用双引号引起来 - Why JsonParser gives double quotes in the return value, using com.google.gson API 使用com.google.code.gson在Java8中创建JSON对象 - Create JSON object in Java8 using com.google.code.gson Gson:如何使用gson将json对象隐蔽到子类对象 - Gson : How to covert json object to child class object using gson 使用Google Gson创建json - create json using Google Gson java maven 导入问题:错误:com.google.gson 包不存在 - java maven import issue: error: package com.google.gson does not exist 编译错误-在Redhat“ Openshift”应用程序中找不到com.google.gson包 - Compilation error - package com.google.gson not found in Redhat “Openshift” application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM