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



是否有可能以某种方式将动态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对象的不同变化。

我不知道我是否理解你的问题。但是,您可以尝试将所有可能的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": 2n" +
"  }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"));

在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"
}
}
}

相关内容

  • 没有找到相关文章

最新更新