如何阅读这种形式的JSON

时间:2019-07-10 07:37:56

标签: java json binding

我以前已经集成了第三方REST API。它的响应曾经是正确的JSON,后来我通过Google GSON将其绑定到我的对象。一切正常。

上一个JSON

{
    "status": 1,
    "msg": "1 out of 1 Transactions Fetched Successfully",
    "transaction_details": {
        "123": {
            "mihpayid": "123",


"request_id": null,
        "bank_ref_num": null,
        "amt": "66.00",
        "transaction_amount": "66.00",
        "txnid": "123",
        "additional_charges": "0.00",
        "productinfo": "Payment Gateway",
        "firstname": "TESTING",
        "bankcode": "asdf",
        "udf1": null,
        "udf3": null,
        "udf4": null,
        "udf5": null,
        "field2": null,
        "field9": null,
        "error_code": null,
        "addedon": "2019-05-24 12:05:07",
        "payment_source": "asdf",
        "card_type": null,
        "error_Message": "",
        "net_amount_debit": "0.00",
        "disc": "0.00",
        "mode": "NB",
        "PG_TYPE": "ASDF",
        "card_no": "",
        "udf2": null,
        "status": "pending",
        "unmappedstatus": "in progress",
        "Merchant_UTR": null,
        "Settled_At": null
    }
}

}

但是,该API最近返回了以下响应,而不是JSON。

新JSON

a:3:{s:6:"status";i:1;s:3:"msg";s:44:"1 out of 1 Transactions Fetched Successfully";s:19:"transaction_details";a:1:{i:123465;a:31:{s:8:"somid";s:10:"1234";s:10:"request_id";s:0:"";s:12:"bank_ref_num";s:10:"125678";s:3:"amt";s:4:"1.00";s:18:"transaction_amount";s:4:"1.00";s:5:"txnid";s:19:"123456";s:18:"additional_charges";s:4:"0.00";s:11:"productinfo";s:15:"Payment Gateway";s:9:"firstname";s:8:"TESTING";s:8:"bankcode";s:4:"INDB";s:4:"udf1";N;s:4:"udf3";N;s:4:"udf4";N;s:4:"udf5";N;s:6:"field2";N;s:6:"field9";N;s:10:"error_code";s:4:"E000";s:7:"addedon";s:19:"2019-07-10 11:08:04";s:14:"payment_source";s:4:"payMe";s:9:"card_type";N;s:13:"error_Message";s:8:"NO ERROR";s:16:"net_amount_debit";d:1;s:4:"disc";s:4:"0.00";s:4:"mode";s:2:"NB";s:7:"PG_TYPE";s:7:"INDB_NB";s:7:"card_no";s:0:"";s:4:"udf2";N;s:6:"status";s:7:"success";s:14:"unmappedstatus";s:8:"captured";s:12:"Merchant_UTR";N;s:10:"Settled_At";s:19:"0000-00-00 00:00:00";}}}

有人对如何将其绑定到Java对象有任何想法

2 个答案:

答案 0 :(得分:1)

响应是一个php序列化的字符串

尝试使用此serialized-php-parser

https://code.google.com/archive/p/serialized-php-parser/downloads

或任何其他php解析器

答案 1 :(得分:0)

如果要将JSON对象转换为Java对象,则需要首先对其进行映射。创建一个包含所有所需变量的类,生成getter和setter以及您可能需要的任何其他方法。现在要将这些JSON对象实际转换为Java,建议您使用 Jackson 库。

在使用Maven的情况下,如何添加它:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>

将库包含在项目中之后,转到程序的主要方法并声明一个 ObjectMapper 对象。

ObjectMapper mapper = new ObjectMapper();

最后:

try {
    YourObject obj = mapper.readValue(new File("YOUR_FILE_PATH"), YourObject.class);
} catch (IOException ex) {
    ex.printStackTrace();
}

如果您以字符串形式收到此响应,则也可以这样做:

try {
    String myJson = "YOUR_JSON_VALUE";
    YourObject obj2 = mapper.readValue(myJson, YourObject.class);
} catch (IOException ex) {
    ex.printStackTrace();
}