我正在尝试从其他API获取一些数据,我需要从JSON对象获取StatusCode,但是我正在获取null对象。
我试图用StatusCode变量创建新类,但是我得到了null。 我正在尝试获取此数据:
Data(data=[{"Number":"20450143160505","DateCreated":"11-06-2019 10:14:27","DocumentWeight":0.5,"CheckWeight":0,"SumBeforeCheckWeight":0,"PayerType":"Recipient","RecipientFullName":"","RecipientDateTime":"","ScheduledDeliveryDate":"12-06-2019","PaymentMethod":"Cash","CargoDescriptionString":"","CargoType":"Parcel","CitySender":"Сокільники","CityRecipient":"Київ","WarehouseRecipient":"Відділення №150 (до 30 кг): вул. Антоновича, 43 (м.\"Олімпійська\")","CounterpartyType":"PrivatePerson","Redelivery":1,"RedeliverySum":"","RedeliveryNum":"","RedeliveryPayer":"","AfterpaymentOnGoodsCost":"","ServiceType":"WarehouseWarehouse","UndeliveryReasonsSubtypeDescription":"","WarehouseRecipientNumber":150,"LastCreatedOnTheBasisNumber":"","LastCreatedOnTheBasisDocumentType":"","LastCreatedOnTheBasisPayerType":"","LastCreatedOnTheBasisDateTime":"","LastTransactionStatusGM":"","LastTransactionDateTimeGM":"","WarehouseRecipientInternetAddressRef":"916c7c93-8460-11e4-acce-0050568002cf","MarketplacePartnerToken":"","DateScan":"12:23 12.06.2019","ClientBarcode":"","SenderAddress":"","RecipientAddress":"","CounterpartySenderDescription":"","CounterpartyRecipientDescription":"","CounterpartySenderType":"Organization","PaymentStatus":"","PaymentStatusDate":"","AmountToPay":"","AmountPaid":"","WarehouseRecipientRef":"916c7c94-8460-11e4-acce-0050568002cf","DocumentCost":40,"AnnouncedPrice":"","OwnerDocumentNumber":"","DateFirstDayStorage":"2019-06-21","InternationalDeliveryType":"","DaysStorageCargo":"","RecipientWarehouseTypeRef":"841339c7-591a-42e2-8233-7a0a00f0ed6f","StorageAmount":"","StoragePrice":"","VolumeWeight":"0.50","SeatsAmount":"1","OwnerDocumentType":"","ActualDeliveryDate":"2019-06-12 12:23:22","DateReturnCargo":"","CardMaskedNumber":"","Status":"Прибув у відділення","StatusCode":"7","RefEW":"8ed817ef-8c18-11e9-91ff-0025b501a04b","RedeliveryPaymentCardRef":"","RedeliveryPaymentCardDescription":"","CreatedOnTheBasis":"","DatePayedKeeping":"2019-06-21 00:00:00","OnlineCreditStatusCode":"","OnlineCreditStatus":""}])
获取数据的方法:
RestTemplate restTemplate = new RestTemplate();
Data string = restTemplate.postForObject(blalba,blabla,Data.class)
我的班级
public class Data {
@JsonProperty("data")
private JsonNode data;
//get set
}
答案 0 :(得分:2)
有几种方法可以实现它:
#include <string>
#include <system_error>
// *NOTE*: if we rename this from 'enums' to 'app' (comment below) and do
// the needed name change changes in the code it will compile just fine
//namespace app
namespace enums
{
// error type enum
enum class Error
{
SignalFail, // failed to set up or execute signal
BadArgument, // bad argument passed to function
InvalidPointer, // Pointer is invalid
NoImplementation, // No implementation
OutOfRange, // Out of range
AlocationFailed, // Failed to alocate memory
MemoryReadFailed, // Failed to read memory
Unexpected // Unexpected execution flow
};
// error condition enum
enum class Condition
{
code_error,
unknown_error
};
}
// Register Error and Condition as error enum and condition enum
namespace std
{
template<>
struct is_error_code_enum<enums::Error> :
public true_type {};
template<>
struct is_error_condition_enum<enums::Condition> :
public true_type {};
}
namespace app
{
// Category types are used to identify the source of an error.
// They also define the relation between error_code and error_condition objects of its category,
// as well as the message set for error_code objects.
class error_category_t :
public std::error_category
{
public:
error_category_t() noexcept {};
inline const char* name() const noexcept override;
std::error_condition default_error_condition(int err_value) const noexcept override;
inline bool equivalent(const std::error_code& err_code, int err_value) const noexcept override;
inline bool equivalent(int err_value, const std::error_condition& err_cond) const noexcept override;
std::string message(int ev) const override;
private:
error_category_t(const error_category_t&) = delete;
error_category_t(error_category_t&&) = delete;
error_category_t& operator=(const error_category_t&) = delete;
error_category_t& operator=(error_category_t&&) = delete;
} const error_category;
const char* error_category_t::name() const noexcept
{
return "app category";
}
bool error_category_t::equivalent(const std::error_code& err_code, int err_value) const noexcept
{
return *this == err_code.category() &&
static_cast<int>(default_error_condition(err_code.value()).value()) == err_value;
}
bool error_category_t::equivalent(int err_value, const std::error_condition& err_cond) const noexcept
{
return default_error_condition(err_value) == err_cond;
}
//
// make_error_condition overload to generate custom conditions:
// This function is called by error_condition's constructor for error condition enum types,
// and should be overloaded for all custom error condition enum types in order to
// provide a mechanism to generate the appropriate error_condition objects from them.
//
inline std::error_condition make_error_condition(enums::Condition ec) noexcept
{
return std::error_condition(static_cast<int>(ec), error_category);
}
//
// This function is called by error_code's constructor for error code enum types
//
inline std::error_code make_error_code(enums::Error code) noexcept
{
return std::error_code(static_cast<int>(code), error_category);
}
}
namespace app
{
std::error_condition error_category_t::default_error_condition(int err_value) const noexcept
{
switch (static_cast<enums::Error>(err_value))
{
case enums::Error::SignalFail:
case enums::Error::BadArgument:
case enums::Error::InvalidPointer:
case enums::Error::NoImplementation:
case enums::Error::OutOfRange:
case enums::Error::AlocationFailed:
case enums::Error::MemoryReadFailed:
case enums::Error::Unexpected:
return std::error_condition(enums::Condition::code_error);
default:
return std::error_condition(enums::Condition::unknown_error);
}
}
std::string error_category_t::message(int err_value) const
{
switch (static_cast<enums::Error>(err_value))
{
case enums::Error::SignalFail:
return "Signaling failed";
case enums::Error::BadArgument:
return "Bad Argument";
case enums::Error::InvalidPointer:
return "Invalid pointer";
case enums::Error::NoImplementation:
return "No implementation";
case enums::Error::OutOfRange:
return "Out of range";
case enums::Error::AlocationFailed:
return "Memory allocation failed";
case enums::Error::MemoryReadFailed:
return "Memory read failed";
case enums::Error::Unexpected:
return "Unexpected execution flow";
default:
return "Unknown error";
}
}
}
您可以将响应有效载荷读取为Map<String, Object>
:
Map<String, Object>
定义一个映射所需属性的类:
ParameterizedTypeReference<HashMap<String, Object>> responseType =
new ParameterizedTypeReference<HashMap<String, Object>>() {};
Map<String, Object> responsePayload =
restTemplate.exchange(purchaseRequestDetailsEndpoint, HttpMethod.POST,
new HttpEntity<>(requestPayload), responseType);
String statusCode = responsePayload.get("StatusCode");
并使用上面定义的类读取响应有效负载:
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponsePayload {
@JsonProperty("StatusCode")
private String statusCode;
}
或者,您可以映射所需的属性,并将其余属性存储在映射中:
ResponsePayload responsePayload =
restTemplate.postForObject(uri, request, ResponsePayload.class);
String statusCode = responsePayload.getStatusCode();