Web Services Java中的REST API对象转换

i1icjdpr  于 2022-11-15  发布在  Java
关注(0)|答案(1)|浏览(151)

我已按以下格式请求供应商正文:

{
  "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对象的以下主体请求作为空值而不是字符串使用:

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

我们该怎么做。

mrfwxfqh

mrfwxfqh1#

我假设你想把它解析成一个定义好的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"
}

相关问题