英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论