在Web服务响应中处理NULL对象的最佳方法是什么,以替换App中的"不可用"。
ex。对于以下响应,我的目标是能够用"不可用"之类的字符串替换所有NULL字符串对象:
"data": { "restaurant_id": 1,
"restaurant_name": "m&ms",
"logo": "",
"rate": null,
"price": 55,
"is_open": false,
"address": null,
"image": ""
}
两种方法可以在解析中接近null,这取决于解析的方式。
1(如果您使用POJO类解析,那么最好的解决方案是在Getter方法中检索值检查时。例如:您的转换后的Pojo就像这样
public class Data {
@SerializedName("restaurant_id")
@Expose
private Integer restaurantId;
@SerializedName("restaurant_name")
@Expose
private String restaurantName;
@SerializedName("logo")
@Expose
private String logo;
@SerializedName("rate")
@Expose
private String rate;
@SerializedName("price")
@Expose
private Integer price;
@SerializedName("is_open")
@Expose
private Boolean isOpen;
@SerializedName("address")
@Expose
private String address;
@SerializedName("image")
@Expose
private String image;
public Integer getRestaurantId() {
return restaurantId;
}
public void setRestaurantId(Integer restaurantId) {
this.restaurantId = restaurantId;
}
public String getRestaurantName() {
return restaurantName;
}
public void setRestaurantName(String restaurantName) {
this.restaurantName = restaurantName;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public String getRate() {
return getValue(rate);
}
public void setRate(String rate) {
this.rate = rate;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Boolean getIsOpen() {
return isOpen;
}
public void setIsOpen(Boolean isOpen) {
this.isOpen = isOpen;
}
public String getAddress() {
return getValue(address);
}
public void setAddress(String address) {
this.address = address;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
现在您可以实现自己的自定义方法来检查字符串值是否为null?如果是null,则设置要通过的默认值。例如,我已经检查了地址和费率是
喜欢这个
//It will return "Not Available" if value is null you can set your default value
public static String getValue(String value){
return TextUtils.isEmpty(value)? "Not Available" : value
}
//You can override it like this too.
public static String getValue(String value, String defaultValue){
return TextUtils.isEmpty(value)? defaultValue : value
}
2(如果您不使用直接解析和解析JSON,则
String jsonString = "Paste server response in json";
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject dataJson = jsonObject.getJSONObject("data");
String address = dataJson.optString("address","Not Available");
} catch (JSONException e) {
e.printStackTrace();
}
尝试一下,您可以检查Wheater
示例代码
public class POJO {
@SerializedName("rate")
@Expose
String rate;
@SerializedName("restaurant_name")
@Expose
String restaurant_name;
@SerializedName("image")
@Expose
String image;
@SerializedName("address")
@Expose
String address;
public String getRate() {
if (TextUtils.isEmpty(rate)) {
return "not available";
}
return rate;
}
public void setRate(String rate) {
this.rate = rate;
}
public String getAddress() {
if (TextUtils.isEmpty(address)) {
return "not available";
}
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getRestaurant_name() {
if (TextUtils.isEmpty(restaurant_name)) {
return "not available";
}
return restaurant_name;
}
public void setRestaurant_name(String restaurant_name) {
this.restaurant_name = restaurant_name;
}
public String getImage() {
if (TextUtils.isEmpty(image)) {
return "not available";
}
return image;
}
public void setImage(String image) {
this.image = image;
}
}
=======================================
如果您想手动通过,请尝试此
if(jsonObject.has("rate"))
{
if (jsonObject.isNull("rate"))
{
String rate = "not available";
}else{
String rate =jsonObject.getString("rate");;
}
}
如果您使用的是像杰克逊这样的工具,则默认情况下将在未知属性上失败,除非指定了模型类上的 @JsonIgnoreProperties(ignoreUnknown = true)
(或在"对象映射"的配置中(。
无论如何,我只是说杰克逊是如今在Java中序列化/进行序列化JSON的一种常见方法,默认情况下它将在未知的道具上投掷400 Bad Request
- 也许这是您需要的?
您可以使用JSONObject
对其进行解析,然后检查NULL并进行更新:
String jsonString = "..."; // Your JSON String
// Parse the JSON string into a JSONObject
JSONObject obj = new JSONObject(jsonString);
// Grab all the keys
Iterator<String> iter = obj.keys();
// Check each key
while (iter.hasNext()) {
String key = iter.next();
Object value = obj.get(key);
// IF a key is null set it to "Not available"
if (value == null) {
obj.put(key, "Not available");
}
}
然后,您只使用JSONObject
来填充UI。