gpt4 book ai didi

java - 读取 GSON 对象获取空元素

转载 作者:行者123 更新时间:2023-12-02 03:12:21 25 4
gpt4 key购买 nike

我正在尝试读取 jsonObject 并插入到我的实体中以创建一个新的实体。问题是我得到的 application=null 和 version=0 并不是 json 中的正确数据。

这是我尝试使用 GSON 库的代码:

final Gson gson = new Gson();
VersionDTO jsonObject = gson.fromJson(entity, VersionDTO.class);

try {
versionFilterService.setVersionDTO(jsonObject);
} catch (ServiceException e) {
e.printStackTrace();
}

实体是一个字符串,它的 json 格式如下:

{
"context": {
"location":{
"longt":0,
"lat":0,
"radius":0
},
"accessToken":"asd",
"notificationToken":"dasda",
"requestTime":11111111,
"application":"1 Android Mobile",
"version":1
},
"request":{
"application":"1 Android Mobile",
"version":1
}
}

这是我的 VersionDTO:

public class VersionDTO {
int version;
String application;

public VersionDTO(int version, String application){
this.version = version;
this.application = application;
}

public String getApplication() {
return application;
}

public void setApplication(String application) {
this.application = application;
}

public int getVersion() {
return version;
}

public void setVersion(int version) {
this.version = version;
}

}

最佳答案

您必须更改 VersionDTO 以具有 Request 属性。 Request 属性应定义应用程序和版本属性。

您可以使用这个tool为 JSON 创建 POJO。

-----------------------------------com.example.Context.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Context {

@SerializedName("location")
@Expose
private Location location;
@SerializedName("accessToken")
@Expose
private String accessToken;
@SerializedName("notificationToken")
@Expose
private String notificationToken;
@SerializedName("requestTime")
@Expose
private Integer requestTime;
@SerializedName("application")
@Expose
private String application;
@SerializedName("version")
@Expose
private Integer version;

/**
*
* @return
* The location
*/
public Location getLocation() {
return location;
}

/**
*
* @param location
* The location
*/
public void setLocation(Location location) {
this.location = location;
}

/**
*
* @return
* The accessToken
*/
public String getAccessToken() {
return accessToken;
}

/**
*
* @param accessToken
* The accessToken
*/
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

/**
*
* @return
* The notificationToken
*/
public String getNotificationToken() {
return notificationToken;
}

/**
*
* @param notificationToken
* The notificationToken
*/
public void setNotificationToken(String notificationToken) {
this.notificationToken = notificationToken;
}

/**
*
* @return
* The requestTime
*/
public Integer getRequestTime() {
return requestTime;
}

/**
*
* @param requestTime
* The requestTime
*/
public void setRequestTime(Integer requestTime) {
this.requestTime = requestTime;
}

/**
*
* @return
* The application
*/
public String getApplication() {
return application;
}

/**
*
* @param application
* The application
*/
public void setApplication(String application) {
this.application = application;
}

/**
*
* @return
* The version
*/
public Integer getVersion() {
return version;
}

/**
*
* @param version
* The version
*/
public void setVersion(Integer version) {
this.version = version;
}

}
-----------------------------------com.example.Location.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Location {

@SerializedName("longt")
@Expose
private Integer longt;
@SerializedName("lat")
@Expose
private Integer lat;
@SerializedName("radius")
@Expose
private Integer radius;

/**
*
* @return
* The longt
*/
public Integer getLongt() {
return longt;
}

/**
*
* @param longt
* The longt
*/
public void setLongt(Integer longt) {
this.longt = longt;
}

/**
*
* @return
* The lat
*/
public Integer getLat() {
return lat;
}

/**
*
* @param lat
* The lat
*/
public void setLat(Integer lat) {
this.lat = lat;
}

/**
*
* @return
* The radius
*/
public Integer getRadius() {
return radius;
}

/**
*
* @param radius
* The radius
*/
public void setRadius(Integer radius) {
this.radius = radius;
}

}
-----------------------------------com.example.Request.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Request {

@SerializedName("application")
@Expose
private String application;
@SerializedName("version")
@Expose
private Integer version;

/**
*
* @return
* The application
*/
public String getApplication() {
return application;
}

/**
*
* @param application
* The application
*/
public void setApplication(String application) {
this.application = application;
}

/**
*
* @return
* The version
*/
public Integer getVersion() {
return version;
}

/**
*
* @param version
* The version
*/
public void setVersion(Integer version) {
this.version = version;
}

}
-----------------------------------com.example.VersionDTO.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class VersionDTO {

@SerializedName("context")
@Expose
private Context context;
@SerializedName("request")
@Expose
private Request request;

/**
*
* @return
* The context
*/
public Context getContext() {
return context;
}

/**
*
* @param context
* The context
*/
public void setContext(Context context) {
this.context = context;
}

/**
*
* @return
* The request
*/
public Request getRequest() {
return request;
}

/**
*
* @param request
* The request
*/
public void setRequest(Request request) {
this.request = request;
}

}

关于java - 读取 GSON 对象获取空元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40843107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com