空条件检查在解析包含嵌套对象的JSON数组时无法正常工作。

huangapple go评论77阅读模式
英文:

Null condition check not working in parsing JSON array with nested objects

问题

以下是代码的翻译部分:

import com.google.gson.*;

public class DataParser {
    public static JsonArray parseData(JsonArray data) throws JsonParseException {
		JsonArray dataArray = new JsonArray();
    	for (Object dataObj : data) {
            JsonParser parser = new JsonParser();
            JsonObject obj = (JsonObject) parser.parse(dataObj.toString());
            JsonObject finalDataObj = new JsonObject();            
            Object fields = obj.get("fields");            
            JsonObject fieldsObj = (JsonObject) parser.parse(fields.toString());
			finalDataObj.add("key", obj.get("key"));
            finalDataObj.addProperty("cityName", displayName(fieldsObj.get("cityName")));
            finalDataObj.addProperty("category", displayName(fieldsObj.get("category")));
    		dataArray.add(finalDataObj);
    	}
		return dataArray;
    }
	
	 public static String displayName(Object dataValue){
        if ( dataValue == null){
             return "";
        }
        else {            
            JsonObject obj = (JsonObject)dataValue;
            return obj.get("name").toString();
        }
    }	
}

请注意,翻译中已经将 " 替换为双引号 ",以使代码正确。

英文:

Beginner in Java working on parsing JSON array with nested objects into simplified JSON array using the below code.

It works fine if there are no null values in the object. The null value check appears to be faulty. I've tried other ways like ==null or equals(null) or equals("null") or !=null or !="null" or =="null" as well before this.

import com.google.gson.*;

public class DataParser {
    public static JsonArray parseData(JsonArray data) throws JsonParseException {
		JsonArray dataArray = new JsonArray();
    	for (Object dataObj : data) {
            JsonParser parser = new JsonParser();
            JsonObject obj = (JsonObject) parser.parse(dataObj.toString());
            JsonObject finalDataObj = new JsonObject();            
            Object fields = obj.get("fields");            
            JsonObject fieldsObj = (JsonObject) parser.parse(fields.toString());
			finalDataObj.add("key", obj.get("key"));
            finalIssueObj.addProperty("cityName", displayName(fieldsObj.get("cityName")));
            finalIssueObj.addProperty("category", displayName(fieldsObj.get("category")));
    		dataArray.add(finalDataObj);
    	}
		return dataArray;
    }
	
	 public static String displayName(Object dataValue){
        if ( dataValue == null){
             return "";
        }
        else {            
            JsonObject obj = (JsonObject)dataValue;
            return obj.get("name").toString();
        }
    }	
}

Input:

[{"key":"1232", "fields": {cityName":{"id":"10000","description":"This is city name","iconUrl":"https://iconurls.com","name":"Hyderabad","subtask":false},"updated":"2023-05-31","category":{"description":"This is category of city.","iconUrl":"https://category1.icon.com","name":"Dining"}}},
{"key":"1233", "fields": {cityName":null,"updated":"2023-05-31","category":null}}]

Output:

[{"key" : "1232", "cityName": "Hyderabad", "category" : "Dining"}, 
{"key" : "1233", "cityName": "", "category" : ""} ]`

答案1

得分: 0

在提供的代码中,您正在尝试使用 dataValue == null 来检查空值。然而,在 JSON 对象的情况下,使用 == 来检查 null 不会按预期工作。相反,您可以使用 Gson 库提供的 JsonNull 类来检查空值。

public static String displayName(JsonElement dataValue) {
   if (dataValue == null || dataValue.isJsonNull()) {
       return "";
   } else {
       JsonObject obj = dataValue.getAsJsonObject();
       return obj.get("name").getAsString();
   }
}

另外,在 parseData 方法中,dataObj 的类型应该是 JsonElement 而不是 Object,以避免不必要的转换。

for (JsonElement dataObj : data) {
   JsonParser parser = new JsonParser();
   JsonObject obj = dataObj.getAsJsonObject();
   JsonObject finalDataObj = new JsonObject();

   JsonObject fieldsObj = obj.getAsJsonObject("fields");
   finalDataObj.add("key", obj.get("key"));
   finalDataObj.addProperty("cityName", displayName(fieldsObj.get("cityName")));
   finalDataObj.addProperty("category", displayName(fieldsObj.get("category")));

   dataArray.add(finalDataObj);
}
英文:

In the provided code, you are trying to check for null values using dataValue == null. However, in the case of JSON objects, checking for null using == won't work as expected. Instead, you can use the JsonNull class provided by the Gson library to check for null values.

public static String displayName(JsonElement dataValue) {
   if (dataValue == null || dataValue.isJsonNull()) {
       return "";
   } else {
       JsonObject obj = dataValue.getAsJsonObject();
       return obj.get("name").getAsString();
   }
}

Additionally, dataObj in the parseData method is of type JsonElement instead of Object to avoid unnecessary conversions.

    for (JsonElement dataObj : data) {
       JsonParser parser = new JsonParser();
       JsonObject obj = dataObj.getAsJsonObject();
       JsonObject finalDataObj = new JsonObject();

       JsonObject fieldsObj = obj.getAsJsonObject("fields");
       finalDataObj.add("key", obj.get("key"));
       finalDataObj.addProperty("cityName", displayName(fieldsObj.get("cityName")));
       finalDataObj.addProperty("category", displayName(fieldsObj.get("category")));

       dataArray.add(finalDataObj);
    }

huangapple
  • 本文由 发表于 2023年6月1日 15:08:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定