将动态JSON反序列化为Java对象

mnemlml8  于 2023-03-31  发布在  Java
关注(0)|答案(3)|浏览(161)

有没有可能以某种方式将动态JSON对象(没有固定模式)转换为Java对象?
例如,第一次,我可以在运行时获得以下JSON对象:

{
  "addressDetails": {
    "homeAddresses": [
      {
        "houseNo": "1",
        "city": "2"
      }
    ],
    "currentAddress": {
      "houseNo": "1",
      "city": "2"
    }
  },
  "personalDetails": {
    "birthDetails": {
      "birthPlace": "1",
      "birthMonth": "2"
    }
  },
  "confidentialDetails": {
    "birthDetails": {
      "birthPlace": "1",
      "birthMonth": "2"
    }
  },
  "bankingDetails": {
    "bankName": "1",
    "bankAccountNo": 2
  }
}

下一次,也许是这样的:

{
  "confidentialDetails": {
    "birthDetails": {
      "birthPlace": "1",
      "birthMonth": "2"
    }
  },
  "personalDetails": {
    "birthDetails": {
      "birthPlace": "1",
      "birthMonth": "2"
    }
  }
}

我如何处理所有这些变化?是否可以与Jackson/Gson或其他一些库?
我希望找到一个非常通用的解决方案,可以优雅地处理JSON对象中的不同变化。

mqxuamgl

mqxuamgl1#

我不知道我是否理解了你的问题。但是,你可以尝试将所有可能的JSON对象定义为一个对象,然后使用GSON或其他JSON转换工具序列化它们。像这样:

public class Parent {
    private AddressDetails addressDetails;
    private PersonalDetails personalDetails;
    private PersonalDetails confidentialDetails;
}
class AddressDetails{
    private List<HomeAddresses> homeAddresses;
    private List<CurrentAddress> currentAddresses;
}
class Addresses{
    private String houseNo;
    private String city;
}
class BirthDetails{
    private String birthDetails;
    private String birthMonth;
}
class HomeAddresses extends Addresses{
}
class CurrentAddress extends Addresses{
}
class PersonalDetails extends BirthDetails{
}

或者,如果要定义的类型太多,您可以简单地将JSON字符串转换为JSON对象,以便像这样使用:

String jsonStr = "{\n" +
            "  \"addressDetails\": {\n" +
            "    \"homeAddresses\": [\n" +
            "      {\n" +
            "        \"houseNo\": \"1\",\n" +
            "        \"city\": \"2\"\n" +
            "      }\n" +
            "    ],\n" +
            "    \"currentAddress\": {\n" +
            "      \"houseNo\": \"1\",\n" +
            "      \"city\": \"2\"\n" +
            "    }\n" +
            "  },\n" +
            "  \"personalDetails\": {\n" +
            "    \"birthDetails\": {\n" +
            "      \"birthPlace\": \"1\",\n" +
            "      \"birthMonth\": \"2\"\n" +
            "    }\n" +
            "  },\n" +
            "  \"confidentialDetails\": {\n" +
            "    \"birthDetails\": {\n" +
            "      \"birthPlace\": \"1\",\n" +
            "      \"birthMonth\": \"2\"\n" +
            "    }\n" +
            "  },\n" +
            "  \"bankingDetails\": {\n" +
            "    \"bankName\": \"1\",\n" +
            "    \"bankAccountNo\": 2\n" +
            "  }\n" +
            "}";
    JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
    JSONObject addressDetails = jsonObject.get("addressDetails", JSONObject.class);
    JSONArray homeAddresses = addressDetails.get("homeAddresses", JSONArray.class);
    JSONObject object = homeAddresses.get(0, JSONObject.class);
    System.out.println(object.get("houseNo"));
    System.out.println(object.get("city"));
n3ipq98p

n3ipq98p2#

用Jackson库可以做到这一点,只需读取JSON并用addressDetailsbankingDetails键删除数据。

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;

public class JavaApplication {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        ObjectNode jsonData = (ObjectNode) mapper.readTree("{\"addressDetails\":{\"homeAddresses\":[{\"houseNo\":\"1\",\"city\":\"2\"}],\"currentAddress\":{\"houseNo\":\"1\",\"city\":\"2\"}},\"personalDetails\":{\"birthDetails\":{\"birthPlace\":\"1\",\"birthMonth\":\"2\"}},\"confidentialDetails\":{\"birthDetails\":{\"birthPlace\":\"1\",\"birthMonth\":\"2\"}},\"bankingDetails\":{\"bankName\":\"1\",\"bankAccountNo\":2}}");
        jsonData.remove("addressDetails");
        jsonData.remove("bankingDetails");
        System.out.println(mapper.writeValueAsString(jsonData));
    }
}

输出将为:

{
  "personalDetails" : {
    "birthDetails" : {
      "birthPlace" : "1",
      "birthMonth" : "2"
    }
  },
  "confidentialDetails" : {
    "birthDetails" : {
      "birthPlace" : "1",
      "birthMonth" : "2"
    }
  }
}
kknvjkwl

kknvjkwl3#

您可以将其反序列化为Map<String,Object>

public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper(); // This is an expensive operation. objectMapper is thread-safe so it cane be shared
    String inboundJsonStr = "{\"name\":\"Jane Doe\",\"id\":2,\"is_active\":null}";
    Map<String,Object> jsonObject = objectMapper.readValue(inboundJsonStr, Map.class);
}

ObjectMapper来自Jackson库
sidenote:Java是一种类型化语言,如果你这样做,你将失去语言提供的类型检查的一些好处

相关问题