小编典典

将深度嵌套的json转换为java对象,反之亦然

json

我正在使用JAXB将json转换为java对象。我面临的问题是,它导致我创建了大量的类,这些类除了充当json标签的占位符外没有其他用途。

例如:在json下面考虑:

{
"proposalAggregation": {
    "Buys": {
        "Heading1": {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        },
        "Heading2": {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        }
    },
    "Sells": {
        "Heading1": {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        },
        "Heading2": {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        }
    },
    "TAChanges": {
        "Heading1": {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        },
        "Heading2": {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        }
    },
    "Existing": {
        "Heading1": {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        },
        "Heading2": {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        }
    },
    "Proposed": {
        "Heading1": {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        },
        "Heading2": {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        }
    },
    "PIAChanges": {
        "Heading1": {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        },
        "Heading2": {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        }
    }
}

}

现在要将这个JSON原样反序列化为java对象,这些是我需要创建的java类:

Class ProposalAggregation --> this contains Buys, sells, existing, porposed,TAChanges, PIA

Class Buys extends Calculation
Class Sells extends Calculation
Class Existing extends Calculation
Class proposed extends Calculation
Class TAChanges extends Calculation
Class PIAChanges extends Calculation 
Class Calculation -- > this contains heading1 and heading2

class Heading1
Class Heading2

因此,总共有9个类可以模仿JSON,并且如果我们研究这些类的角色,它们不过是JSON标签的占位符。有什么简单的方法吗?


阅读 898

收藏
2020-07-27

共1个答案

小编典典

您可以通过以下方式实现Google的GSON库。已经显示了一个示例类,您可以进一步对其进行修改。

类-提案汇总

package com.test;

import java.util.Map;

public class ProposalAggregation {

private Map<String, Map<String, String>> Buys;
private Map<String, Map<String, String>> Sells;
private Map<String, Map<String, String>> TAChanges;

public Map<String, Map<String, String>> getBuys() {
    return Buys;
}

public void setBuys(Map<String, Map<String, String>> buys) {
    Buys = buys;
}

public Map<String, Map<String, String>> getSells() {
    return Sells;
}

public void setSells(Map<String, Map<String, String>> sells) {
    Sells = sells;
}

public Map<String, Map<String, String>> getTAChanges() {
    return TAChanges;
}

public void setTAChanges(Map<String, Map<String, String>> tAChanges) {
    TAChanges = tAChanges;
}

public String toString() {
    return "BUYS=" + this.Buys + " \nSELLS=" + this.Sells + " \nTACHANGES="
            + this.TAChanges;
}

}

测试主类 请注意,我已经修改了key名称,只是为了验证转换是否正确。

package com.test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import com.google.gson.Gson;

public class TestClass {

    public static void main(String[] args) {
        Gson gson = new Gson();

        try {

            String jsonString = " { 'Buys': { 'bHeading1': { 'bkey1': 'value1', 'bkey2': 'value2', 'bkey3': 'value3' }, 'bbHeading2': { 'bbkey1': 'value1', 'bbkey2': 'value2', 'bbkey3': 'value3' } }, "
                    + "'Sells': { 'sHeading1': { 'skey1': 'value1', 'skey2': 'value2', 'skey3': 'value3' }, 'ssHeading2': { 'sskey1': 'value1', 'sskey2': 'value2', 'sskey3': 'value3' } }, "
                    + "'TAChanges': { 'Heading1': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' }, 'Heading2': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' } }, 'Existing': { 'Heading1': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' }, 'Heading2': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' } }, 'Proposed': { 'Heading1': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' }, 'Heading2': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' } }, 'PIAChanges': { 'Heading1': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' }, 'Heading2': { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' } } }";


            // convert to java class
            ProposalAggregation obj = gson.fromJson(jsonString, ProposalAggregation.class);
            System.out.println("OBJECT : " + obj);

            // convert to json
            String jsonStringFromObj = gson.toJson(obj);
            System.out.println("JSON : " + jsonStringFromObj);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

这是输出。

OBJECT : BUYS={bHeading1={bkey1=value1, bkey2=value2, bkey3=value3}, bbHeading2={bbkey1=value1, bbkey2=value2, bbkey3=value3}} 
SELLS={sHeading1={skey1=value1, skey2=value2, skey3=value3}, ssHeading2={sskey1=value1, sskey2=value2, sskey3=value3}} 
TACHANGES={Heading1={key1=value1, key2=value2, key3=value3}, Heading2={key1=value1, key2=value2, key3=value3}}
JSON : {"Buys":{"bHeading1":{"bkey1":"value1","bkey2":"value2","bkey3":"value3"},"bbHeading2":{"bbkey1":"value1","bbkey2":"value2","bbkey3":"value3"}},"Sells":{"sHeading1":{"skey1":"value1","skey2":"value2","skey3":"value3"},"ssHeading2":{"sskey1":"value1","sskey2":"value2","sskey3":"value3"}},"TAChanges":{"Heading1":{"key1":"value1","key2":"value2","key3":"value3"},"Heading2":{"key1":"value1","key2":"value2","key3":"value3"}}}
2020-07-27