您可以通过以下方式实现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"}}}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)