将动态JSON字符串转换为HashMap

时间:2017-03-09 11:32:02

标签: java json json-deserialization

我有一个带有一些动态属性的JSONObject,我想把它转换成一个类,我在SO上尝试了很多例子,但没有解决方案。

我的json字符串看起来像这样

{
"result": {
    "account": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
    "assets": {
        "r9F6wk8HkXrgYWoJ7fsv4VrUBVoqDVtzkH": [
            {
                "currency": "BTC",
                "value": "5444166510000000e-26"
            }
        ],
        "rPFLkxQk6xUGdGYEykqe7PR25Gr7mLHDc8": [
            {
                "currency": "EUR",
                "value": "4000000000000000e-27"
            }
        ],
        "rPU6VbckqCLW4kb51CWqZdxvYyQrQVsnSj": [
            {
                "currency": "BTC",
                "value": "1029900000000000e-26"
            }
        ],
        "rpR95n1iFkTqpoy1e878f4Z1pVHVtWKMNQ": [
            {
                "currency": "BTC",
                "value": "4000000000000000e-30"
            }
        ],
        "rwmUaXsWtXU4Z843xSYwgt1is97bgY8yj6": [
            {
                "currency": "BTC",
                "value": "8700000000000000e-30"
            }
        ]
    },
    "balances": {
        "rKm4uWpg9tfwbVSeATv4KxDe6mpE9yPkgJ": [
            {
                "currency": "EUR",
                "value": "29826.1965999999"
            }
        ],
        "ra7JkEzrgeKHdzKgo4EUUVBnxggY4z37kt": [
            {
                "currency": "USD",
                "value": "13857.70416"
            }
        ]
    },
    "ledger_hash": "980FECF48CA4BFDEC896692C31A50D484BDFE865EC101B00259C413AA3DBD672",
    "ledger_index": 14483212,
    "obligations": {
        "BTC": "5908.324927635318",
        "EUR": "992471.7419793958",
        "GBP": "4991.38706013193",
        "USD": "1997134.20229482"
    },
    "status": "success",
    "validated": true
}
}

我可以在json.org或O​​bjectMapper中使用吗?

给我问题的唯一部分是资产和余额,我将在正确的方向感谢所有帮助

1 个答案:

答案 0 :(得分:1)

您应该能够将其反序列化为类:

public class Response {
    private Result result;
}

public class Result {
    private String account;
    private Map<String, List<Asset>> assets;
    private Map<String, List<Asset>> balances;
    private String ledger_hash;
    private String ledger_index;
    private Map<String, String> obligations;
    private String status;
    private boolean validated;
}

public class Asset {
    private String currency;
    private String value;
}