java中的REST API对象转换



我有以下格式的供应商请求正文:

{
"supplier": {
"supplierData": {
"supplierAddress": {
"street": "abc",
"postCode": "1234",
"city1": "abcd",
"country": "DE",
"region": "BW"
},
"location": {
"locationID": "1234",
"locationName": "South Africa "
},
"lastUpdatedDateTime": "2022-06-28T10:07:37.000Z"
}    
}
}

能够使用它,并且需要知道如何使用以下Location对象的主体请求为null而不是字符串:

{
"supplier": {
"supplierData": {
"supplierAddress": {
"street": "abc",
"postCode": "1234",
"city1": "abcd",
"country": "DE",
"region": "BW"
},
"location": "null",
"lastUpdatedDateTime": "2022-06-28T10:07:37.000Z"
}    
}
}

你知道我们该怎么做吗?

我假设您想将其解析为已定义的POJO。并且假设Location必须是Object,否则(一个string),它是null,然后您可以尝试解析它:

JSONObject jo = new JSONObject(jsonString); //use your parser library method
Location location = null;
try{
JSONObject jo2 = jo.getJsonObject(location);
//parse jo2 into location
}
catch(JSONException e){
//if exception is throw, it would mean the location data is a string, which can only be "null"
}

相关内容

  • 没有找到相关文章

最新更新