如何在Java中动态嵌套修改JSON?
How to modify JSON with dynamic nesting in Java?
我正在使用 QuickBooks 报告 API,我想将 JSON 报告转换为 pdf 格式并进行一些修改。大多数报告(如 Balance Sheet)具有动态嵌套结构,即嵌套深度不是静态的。如何将 JSON 对象转换为 Java 对象,以便我可以处理它们。总的来说,一些行构成部分,一个部分可以包含行或子部分(这种嵌套可以更深)。区分的方法是在行值中存在类型 data
或 section
。
我正在考虑编写一个递归函数,它遍历行直到找到包含 type=data
的行。但我正在寻找更好的东西。
Link 至 QuickBooks 文档。
这是示例 JSON。
{
"Header": {
"ReportName": "BalanceSheet",
"Option": [
{
"Name": "AccountingStandard",
"Value": "GAAP"
},
{
"Name": "NoReportData",
"Value": "false"
}
],
"DateMacro": "this calendar year-to-date",
"ReportBasis": "Accrual",
"StartPeriod": "2016-01-01",
"Currency": "USD",
"EndPeriod": "2016-10-31",
"Time": "2016-10-31T09:42:21-07:00",
"SummarizeColumnsBy": "Total"
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "ASSETS"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Current Assets"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Bank Accounts"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "35",
"value": "Checking"
},
{
"value": "1350.55"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "36",
"value": "Savings"
},
{
"value": "800.00"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "BankAccounts",
"Summary": {
"ColData": [
{
"value": "Total Bank Accounts"
},
{
"value": "2150.55"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Accounts Receivable"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "84",
"value": "Accounts Receivable (A/R)"
},
{
"value": "6383.12"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "AR",
"Summary": {
"ColData": [
{
"value": "Total Accounts Receivable"
},
{
"value": "6383.12"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Other current assets"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "81",
"value": "Inventory Asset"
},
{
"value": "596.25"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "4",
"value": "Undeposited Funds"
},
{
"value": "2117.52"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "OtherCurrentAssets",
"Summary": {
"ColData": [
{
"value": "Total Other current assets"
},
{
"value": "2713.77"
}
]
}
}
]
},
"type": "Section",
"group": "CurrentAssets",
"Summary": {
"ColData": [
{
"value": "Total Current Assets"
},
{
"value": "11247.44"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Fixed Assets"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"id": "37",
"value": "Truck"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "38",
"value": "Original Cost"
},
{
"value": "13495.00"
}
],
"type": "Data"
}
]
},
"type": "Section",
"Summary": {
"ColData": [
{
"value": "Total Truck"
},
{
"value": "13495.00"
}
]
}
}
]
},
"type": "Section",
"group": "FixedAssets",
"Summary": {
"ColData": [
{
"value": "Total Fixed Assets"
},
{
"value": "13495.00"
}
]
}
}
]
},
"type": "Section",
"group": "TotalAssets",
"Summary": {
"ColData": [
{
"value": "TOTAL ASSETS"
},
{
"value": "24742.44"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "LIABILITIES AND EQUITY"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Liabilities"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Current Liabilities"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Accounts Payable"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "33",
"value": "Accounts Payable (A/P)"
},
{
"value": "1984.17"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "AP",
"Summary": {
"ColData": [
{
"value": "Total Accounts Payable"
},
{
"value": "1984.17"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Credit Cards"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "41",
"value": "Mastercard"
},
{
"value": "157.72"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "CreditCards",
"Summary": {
"ColData": [
{
"value": "Total Credit Cards"
},
{
"value": "157.72"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Other Current Liabilities"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "89",
"value": "Arizona Dept. of Revenue Payable"
},
{
"value": "4.55"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "90",
"value": "Board of Equalization Payable"
},
{
"value": "401.98"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "43",
"value": "Loan Payable"
},
{
"value": "4000.00"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "OtherCurrentLiabilities",
"Summary": {
"ColData": [
{
"value": "Total Other Current Liabilities"
},
{
"value": "4406.53"
}
]
}
}
]
},
"type": "Section",
"group": "CurrentLiabilities",
"Summary": {
"ColData": [
{
"value": "Total Current Liabilities"
},
{
"value": "6548.42"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Long-Term Liabilities"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "44",
"value": "Notes Payable"
},
{
"value": "25000.00"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "LongTermLiabilities",
"Summary": {
"ColData": [
{
"value": "Total Long-Term Liabilities"
},
{
"value": "25000.00"
}
]
}
}
]
},
"type": "Section",
"group": "Liabilities",
"Summary": {
"ColData": [
{
"value": "Total Liabilities"
},
{
"value": "31548.42"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Equity"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "34",
"value": "Opening Balance Equity"
},
{
"value": "-9337.50"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "2",
"value": "Retained Earnings"
},
{
"value": "91.25"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Net Income"
},
{
"value": "2440.27"
}
],
"type": "Data",
"group": "NetIncome"
}
]
},
"type": "Section",
"group": "Equity",
"Summary": {
"ColData": [
{
"value": "Total Equity"
},
{
"value": "-6805.98"
}
]
}
}
]
},
"type": "Section",
"group": "TotalLiabilitiesAndEquity",
"Summary": {
"ColData": [
{
"value": "TOTAL LIABILITIES AND EQUITY"
},
{
"value": "24742.44"
}
]
}
}
]
},
"Columns": {
"Column": [
{
"ColType": "Account",
"ColTitle": "",
"MetaData": [
{
"Name": "ColKey",
"Value": "account"
}
]
},
{
"ColType": "Money",
"ColTitle": "Total",
"MetaData": [
{
"Name": "ColKey",
"Value": "total"
}
]
}
]
}
}
在 java 中使用反射 apis 创建您的自定义类型:
Type myType = new TypeToken<ArrayList<HashMap<String, Object>>() {}.getType();
然后使用Gson api将JSON转换成Java对象:
List<Map<String, Object>> outputList = new Gson().fromJson(inputJSONObject, myType);
然后您可以遍历您的列表并创建一些消费者以从中获取子对象:
UnaryOperator<Map<String, Object>> subMap = s -> {
Map<String, Object> map = new HashMap<>();
map.put("Header", s.remove("Header"));
...
...
return map;
};
JSON 对象可以总是 转换为地图。 JSON 列表可以总是 转换为列表。 Object
在这些结构中可以是 Map<String, Object>
或 List<Object>
。所以你得到了没有深度限制的多嵌套结构。 JSON转Javaclass和Javaclass转JSON分别称为反序列化和序列化。今天有 2 个主要的已知库可以为您做这件事(以及更多)。一个是 Jackson-JSON(也称为 "Fast XML" - 算了吧...),另一个是 GSON - 一个基于 Jackson-JSON 的 Google 库,但也支持传递二进制对象。我个人更喜欢Jackson-JSON,但这是个人喜好问题。对于 GSON 库,请查看 here. For Jackson look here. For Maven artifacts for Jackson look here. If you choose to work with Jackson, then the main class that you need is ObjectMapper。
从研究方法 readValue()
和 writeValue()
开始。在网络上寻找有关如何使用它的无数示例。这应该会给你一个好的开始
这是一个超级嵌套的 JSON 对象。这使得 Java 中的操作变得复杂并且难以排除故障。尽管如此,它还是可以做到的。
您可以使用 BSON 加载您的代码,然后解析出这些值。
从导入您的库开始。
//import java.util.ArrayList;
//import org.bson.Document;
将您的 JSON 解析为 BSON 文档
Document root = Document.parse("{ \"Header\" : { \"ReportName\" : \"BalanceSheet\", \"Option\" : [{ \"Name\" : \"AccountingStandard\", \"Value\" : \"GAAP\" }, { \"Name\" : \"NoReportData\", \"Value\" : \"false\" }], \"DateMacro\" : \"this calendar year-to-date\", \"ReportBasis\" : \"Accrual\", \"StartPeriod\" : \"2016-01-01\", \"Currency\" : \"USD\", \"EndPeriod\" : \"2016-10-31\", \"Time\" : \"2016-10-31T09:42:21-07:00\", \"SummarizeColumnsBy\" : \"Total\" }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"value\" : \"ASSETS\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"value\" : \"Current Assets\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"value\" : \"Bank Accounts\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"35\", \"value\" : \"Checking\" }, { \"value\" : \"1350.55\" }], \"type\" : \"Data\" }, { \"ColData\" : [{ \"id\" : \"36\", \"value\" : \"Savings\" }, { \"value\" : \"800.00\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"BankAccounts\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Bank Accounts\" }, { \"value\" : \"2150.55\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Accounts Receivable\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"84\", \"value\" : \"Accounts Receivable (A/R)\" }, { \"value\" : \"6383.12\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"AR\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Accounts Receivable\" }, { \"value\" : \"6383.12\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Other current assets\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"81\", \"value\" : \"Inventory Asset\" }, { \"value\" : \"596.25\" }], \"type\" : \"Data\" }, { \"ColData\" : [{ \"id\" : \"4\", \"value\" : \"Undeposited Funds\" }, { \"value\" : \"2117.52\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"OtherCurrentAssets\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Other current assets\" }, { \"value\" : \"2713.77\" }] } }] }, \"type\" : \"Section\", \"group\" : \"CurrentAssets\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Current Assets\" }, { \"value\" : \"11247.44\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Fixed Assets\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"id\" : \"37\", \"value\" : \"Truck\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"38\", \"value\" : \"Original Cost\" }, { \"value\" : \"13495.00\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Truck\" }, { \"value\" : \"13495.00\" }] } }] }, \"type\" : \"Section\", \"group\" : \"FixedAssets\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Fixed Assets\" }, { \"value\" : \"13495.00\" }] } }] }, \"type\" : \"Section\", \"group\" : \"TotalAssets\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"TOTAL ASSETS\" }, { \"value\" : \"24742.44\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"LIABILITIES AND EQUITY\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"value\" : \"Liabilities\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"value\" : \"Current Liabilities\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"value\" : \"Accounts Payable\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"33\", \"value\" : \"Accounts Payable (A/P)\" }, { \"value\" : \"1984.17\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"AP\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Accounts Payable\" }, { \"value\" : \"1984.17\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Credit Cards\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"41\", \"value\" : \"Mastercard\" }, { \"value\" : \"157.72\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"CreditCards\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Credit Cards\" }, { \"value\" : \"157.72\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Other Current Liabilities\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"89\", \"value\" : \"Arizona Dept. of Revenue Payable\" }, { \"value\" : \"4.55\" }], \"type\" : \"Data\" }, { \"ColData\" : [{ \"id\" : \"90\", \"value\" : \"Board of Equalization Payable\" }, { \"value\" : \"401.98\" }], \"type\" : \"Data\" }, { \"ColData\" : [{ \"id\" : \"43\", \"value\" : \"Loan Payable\" }, { \"value\" : \"4000.00\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"OtherCurrentLiabilities\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Other Current Liabilities\" }, { \"value\" : \"4406.53\" }] } }] }, \"type\" : \"Section\", \"group\" : \"CurrentLiabilities\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Current Liabilities\" }, { \"value\" : \"6548.42\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Long-Term Liabilities\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"44\", \"value\" : \"Notes Payable\" }, { \"value\" : \"25000.00\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"LongTermLiabilities\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Long-Term Liabilities\" }, { \"value\" : \"25000.00\" }] } }] }, \"type\" : \"Section\", \"group\" : \"Liabilities\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Liabilities\" }, { \"value\" : \"31548.42\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Equity\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"34\", \"value\" : \"Opening Balance Equity\" }, { \"value\" : \"-9337.50\" }], \"type\" : \"Data\" }, { \"ColData\" : [{ \"id\" : \"2\", \"value\" : \"Retained Earnings\" }, { \"value\" : \"91.25\" }], \"type\" : \"Data\" }, { \"ColData\" : [{ \"value\" : \"Net Income\" }, { \"value\" : \"2440.27\" }], \"type\" : \"Data\", \"group\" : \"NetIncome\" }] }, \"type\" : \"Section\", \"group\" : \"Equity\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Equity\" }, { \"value\" : \"-6805.98\" }] } }] }, \"type\" : \"Section\", \"group\" : \"TotalLiabilitiesAndEquity\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"TOTAL LIABILITIES AND EQUITY\" }, { \"value\" : \"24742.44\" }] } }] }, \"Columns\" : { \"Column\" : [{ \"ColType\" : \"Account\", \"ColTitle\" : \"\", \"MetaData\" : [{ \"Name\" : \"ColKey\", \"Value\" : \"account\" }] }, { \"ColType\" : \"Money\", \"ColTitle\" : \"Total\", \"MetaData\" : [{ \"Name\" : \"ColKey\", \"Value\" : \"total\" }] }] } }");
然后您可以使用以下语法调出这些值。对于数组,您可以遍历值的数量。为了使其工作,您需要知道返回的值是什么 (I.E. Int, String, or Document) 。 BSON returns 值作为 "Object" 所以你必须转换值。例如:(字符串)bson.get("field")。这个JSON超级大。下面是一些如何在一行中解析的示例。
System.out.println(((String)((Document)root.get("Header")).get("ReportName")));
System.out.println(((String)((Document)((ArrayList)((Document)root.get("Header")).get("Option")).get(0)).get("Name")));
System.out.println(((String)((Document)((ArrayList)((Document)root.get("Header")).get("Option")).get(0)).get("Value")));
System.out.println(((String)((Document)root.get("Header")).get("DateMacro")));
System.out.println(((String)((Document)root.get("Header")).get("ReportBasis")));
System.out.println(((String)((Document)root.get("Header")).get("StartPeriod")));
System.out.println(((String)((Document)root.get("Header")).get("Currency")));
System.out.println(((String)((Document)root.get("Header")).get("EndPeriod")));
System.out.println(((String)((Document)root.get("Header")).get("Time")));
System.out.println(((String)((Document)root.get("Header")).get("SummarizeColumnsBy")));
System.out.println(((String)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)root.get("Rows")).get("Row")).get(0)).get("Header")).get("ColData")).get(0)).get("value")));
System.out.println(((String)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)root.get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Header")).get("ColData")).get(0)).get("value")));
System.out.println(((String)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)root.get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Header")).get("ColData")).get(0)).get("value")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)root.get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("ColData")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)root.get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("ColData")).get(0)).get("value")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)root.get("Columns")).get("Column")).get(1)).get("MetaData")).get(0)).get("Name")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)root.get("Columns")).get("Column")).get(1)).get("MetaData")).get(0)).get("Value")));
我正在使用 QuickBooks 报告 API,我想将 JSON 报告转换为 pdf 格式并进行一些修改。大多数报告(如 Balance Sheet)具有动态嵌套结构,即嵌套深度不是静态的。如何将 JSON 对象转换为 Java 对象,以便我可以处理它们。总的来说,一些行构成部分,一个部分可以包含行或子部分(这种嵌套可以更深)。区分的方法是在行值中存在类型 data
或 section
。
我正在考虑编写一个递归函数,它遍历行直到找到包含 type=data
的行。但我正在寻找更好的东西。
Link 至 QuickBooks 文档。
这是示例 JSON。
{
"Header": {
"ReportName": "BalanceSheet",
"Option": [
{
"Name": "AccountingStandard",
"Value": "GAAP"
},
{
"Name": "NoReportData",
"Value": "false"
}
],
"DateMacro": "this calendar year-to-date",
"ReportBasis": "Accrual",
"StartPeriod": "2016-01-01",
"Currency": "USD",
"EndPeriod": "2016-10-31",
"Time": "2016-10-31T09:42:21-07:00",
"SummarizeColumnsBy": "Total"
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "ASSETS"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Current Assets"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Bank Accounts"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "35",
"value": "Checking"
},
{
"value": "1350.55"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "36",
"value": "Savings"
},
{
"value": "800.00"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "BankAccounts",
"Summary": {
"ColData": [
{
"value": "Total Bank Accounts"
},
{
"value": "2150.55"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Accounts Receivable"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "84",
"value": "Accounts Receivable (A/R)"
},
{
"value": "6383.12"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "AR",
"Summary": {
"ColData": [
{
"value": "Total Accounts Receivable"
},
{
"value": "6383.12"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Other current assets"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "81",
"value": "Inventory Asset"
},
{
"value": "596.25"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "4",
"value": "Undeposited Funds"
},
{
"value": "2117.52"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "OtherCurrentAssets",
"Summary": {
"ColData": [
{
"value": "Total Other current assets"
},
{
"value": "2713.77"
}
]
}
}
]
},
"type": "Section",
"group": "CurrentAssets",
"Summary": {
"ColData": [
{
"value": "Total Current Assets"
},
{
"value": "11247.44"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Fixed Assets"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"id": "37",
"value": "Truck"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "38",
"value": "Original Cost"
},
{
"value": "13495.00"
}
],
"type": "Data"
}
]
},
"type": "Section",
"Summary": {
"ColData": [
{
"value": "Total Truck"
},
{
"value": "13495.00"
}
]
}
}
]
},
"type": "Section",
"group": "FixedAssets",
"Summary": {
"ColData": [
{
"value": "Total Fixed Assets"
},
{
"value": "13495.00"
}
]
}
}
]
},
"type": "Section",
"group": "TotalAssets",
"Summary": {
"ColData": [
{
"value": "TOTAL ASSETS"
},
{
"value": "24742.44"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "LIABILITIES AND EQUITY"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Liabilities"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Current Liabilities"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Accounts Payable"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "33",
"value": "Accounts Payable (A/P)"
},
{
"value": "1984.17"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "AP",
"Summary": {
"ColData": [
{
"value": "Total Accounts Payable"
},
{
"value": "1984.17"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Credit Cards"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "41",
"value": "Mastercard"
},
{
"value": "157.72"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "CreditCards",
"Summary": {
"ColData": [
{
"value": "Total Credit Cards"
},
{
"value": "157.72"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Other Current Liabilities"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "89",
"value": "Arizona Dept. of Revenue Payable"
},
{
"value": "4.55"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "90",
"value": "Board of Equalization Payable"
},
{
"value": "401.98"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "43",
"value": "Loan Payable"
},
{
"value": "4000.00"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "OtherCurrentLiabilities",
"Summary": {
"ColData": [
{
"value": "Total Other Current Liabilities"
},
{
"value": "4406.53"
}
]
}
}
]
},
"type": "Section",
"group": "CurrentLiabilities",
"Summary": {
"ColData": [
{
"value": "Total Current Liabilities"
},
{
"value": "6548.42"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Long-Term Liabilities"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "44",
"value": "Notes Payable"
},
{
"value": "25000.00"
}
],
"type": "Data"
}
]
},
"type": "Section",
"group": "LongTermLiabilities",
"Summary": {
"ColData": [
{
"value": "Total Long-Term Liabilities"
},
{
"value": "25000.00"
}
]
}
}
]
},
"type": "Section",
"group": "Liabilities",
"Summary": {
"ColData": [
{
"value": "Total Liabilities"
},
{
"value": "31548.42"
}
]
}
},
{
"Header": {
"ColData": [
{
"value": "Equity"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"id": "34",
"value": "Opening Balance Equity"
},
{
"value": "-9337.50"
}
],
"type": "Data"
},
{
"ColData": [
{
"id": "2",
"value": "Retained Earnings"
},
{
"value": "91.25"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Net Income"
},
{
"value": "2440.27"
}
],
"type": "Data",
"group": "NetIncome"
}
]
},
"type": "Section",
"group": "Equity",
"Summary": {
"ColData": [
{
"value": "Total Equity"
},
{
"value": "-6805.98"
}
]
}
}
]
},
"type": "Section",
"group": "TotalLiabilitiesAndEquity",
"Summary": {
"ColData": [
{
"value": "TOTAL LIABILITIES AND EQUITY"
},
{
"value": "24742.44"
}
]
}
}
]
},
"Columns": {
"Column": [
{
"ColType": "Account",
"ColTitle": "",
"MetaData": [
{
"Name": "ColKey",
"Value": "account"
}
]
},
{
"ColType": "Money",
"ColTitle": "Total",
"MetaData": [
{
"Name": "ColKey",
"Value": "total"
}
]
}
]
}
}
在 java 中使用反射 apis 创建您的自定义类型:
Type myType = new TypeToken<ArrayList<HashMap<String, Object>>() {}.getType();
然后使用Gson api将JSON转换成Java对象:
List<Map<String, Object>> outputList = new Gson().fromJson(inputJSONObject, myType);
然后您可以遍历您的列表并创建一些消费者以从中获取子对象:
UnaryOperator<Map<String, Object>> subMap = s -> {
Map<String, Object> map = new HashMap<>();
map.put("Header", s.remove("Header"));
...
...
return map;
};
JSON 对象可以总是 转换为地图。 JSON 列表可以总是 转换为列表。 Object
在这些结构中可以是 Map<String, Object>
或 List<Object>
。所以你得到了没有深度限制的多嵌套结构。 JSON转Javaclass和Javaclass转JSON分别称为反序列化和序列化。今天有 2 个主要的已知库可以为您做这件事(以及更多)。一个是 Jackson-JSON(也称为 "Fast XML" - 算了吧...),另一个是 GSON - 一个基于 Jackson-JSON 的 Google 库,但也支持传递二进制对象。我个人更喜欢Jackson-JSON,但这是个人喜好问题。对于 GSON 库,请查看 here. For Jackson look here. For Maven artifacts for Jackson look here. If you choose to work with Jackson, then the main class that you need is ObjectMapper。
从研究方法 readValue()
和 writeValue()
开始。在网络上寻找有关如何使用它的无数示例。这应该会给你一个好的开始
这是一个超级嵌套的 JSON 对象。这使得 Java 中的操作变得复杂并且难以排除故障。尽管如此,它还是可以做到的。
您可以使用 BSON 加载您的代码,然后解析出这些值。 从导入您的库开始。
//import java.util.ArrayList;
//import org.bson.Document;
将您的 JSON 解析为 BSON 文档
Document root = Document.parse("{ \"Header\" : { \"ReportName\" : \"BalanceSheet\", \"Option\" : [{ \"Name\" : \"AccountingStandard\", \"Value\" : \"GAAP\" }, { \"Name\" : \"NoReportData\", \"Value\" : \"false\" }], \"DateMacro\" : \"this calendar year-to-date\", \"ReportBasis\" : \"Accrual\", \"StartPeriod\" : \"2016-01-01\", \"Currency\" : \"USD\", \"EndPeriod\" : \"2016-10-31\", \"Time\" : \"2016-10-31T09:42:21-07:00\", \"SummarizeColumnsBy\" : \"Total\" }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"value\" : \"ASSETS\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"value\" : \"Current Assets\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"value\" : \"Bank Accounts\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"35\", \"value\" : \"Checking\" }, { \"value\" : \"1350.55\" }], \"type\" : \"Data\" }, { \"ColData\" : [{ \"id\" : \"36\", \"value\" : \"Savings\" }, { \"value\" : \"800.00\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"BankAccounts\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Bank Accounts\" }, { \"value\" : \"2150.55\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Accounts Receivable\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"84\", \"value\" : \"Accounts Receivable (A/R)\" }, { \"value\" : \"6383.12\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"AR\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Accounts Receivable\" }, { \"value\" : \"6383.12\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Other current assets\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"81\", \"value\" : \"Inventory Asset\" }, { \"value\" : \"596.25\" }], \"type\" : \"Data\" }, { \"ColData\" : [{ \"id\" : \"4\", \"value\" : \"Undeposited Funds\" }, { \"value\" : \"2117.52\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"OtherCurrentAssets\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Other current assets\" }, { \"value\" : \"2713.77\" }] } }] }, \"type\" : \"Section\", \"group\" : \"CurrentAssets\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Current Assets\" }, { \"value\" : \"11247.44\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Fixed Assets\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"id\" : \"37\", \"value\" : \"Truck\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"38\", \"value\" : \"Original Cost\" }, { \"value\" : \"13495.00\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Truck\" }, { \"value\" : \"13495.00\" }] } }] }, \"type\" : \"Section\", \"group\" : \"FixedAssets\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Fixed Assets\" }, { \"value\" : \"13495.00\" }] } }] }, \"type\" : \"Section\", \"group\" : \"TotalAssets\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"TOTAL ASSETS\" }, { \"value\" : \"24742.44\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"LIABILITIES AND EQUITY\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"value\" : \"Liabilities\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"value\" : \"Current Liabilities\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"Header\" : { \"ColData\" : [{ \"value\" : \"Accounts Payable\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"33\", \"value\" : \"Accounts Payable (A/P)\" }, { \"value\" : \"1984.17\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"AP\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Accounts Payable\" }, { \"value\" : \"1984.17\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Credit Cards\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"41\", \"value\" : \"Mastercard\" }, { \"value\" : \"157.72\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"CreditCards\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Credit Cards\" }, { \"value\" : \"157.72\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Other Current Liabilities\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"89\", \"value\" : \"Arizona Dept. of Revenue Payable\" }, { \"value\" : \"4.55\" }], \"type\" : \"Data\" }, { \"ColData\" : [{ \"id\" : \"90\", \"value\" : \"Board of Equalization Payable\" }, { \"value\" : \"401.98\" }], \"type\" : \"Data\" }, { \"ColData\" : [{ \"id\" : \"43\", \"value\" : \"Loan Payable\" }, { \"value\" : \"4000.00\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"OtherCurrentLiabilities\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Other Current Liabilities\" }, { \"value\" : \"4406.53\" }] } }] }, \"type\" : \"Section\", \"group\" : \"CurrentLiabilities\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Current Liabilities\" }, { \"value\" : \"6548.42\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Long-Term Liabilities\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"44\", \"value\" : \"Notes Payable\" }, { \"value\" : \"25000.00\" }], \"type\" : \"Data\" }] }, \"type\" : \"Section\", \"group\" : \"LongTermLiabilities\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Long-Term Liabilities\" }, { \"value\" : \"25000.00\" }] } }] }, \"type\" : \"Section\", \"group\" : \"Liabilities\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Liabilities\" }, { \"value\" : \"31548.42\" }] } }, { \"Header\" : { \"ColData\" : [{ \"value\" : \"Equity\" }, { \"value\" : \"\" }] }, \"Rows\" : { \"Row\" : [{ \"ColData\" : [{ \"id\" : \"34\", \"value\" : \"Opening Balance Equity\" }, { \"value\" : \"-9337.50\" }], \"type\" : \"Data\" }, { \"ColData\" : [{ \"id\" : \"2\", \"value\" : \"Retained Earnings\" }, { \"value\" : \"91.25\" }], \"type\" : \"Data\" }, { \"ColData\" : [{ \"value\" : \"Net Income\" }, { \"value\" : \"2440.27\" }], \"type\" : \"Data\", \"group\" : \"NetIncome\" }] }, \"type\" : \"Section\", \"group\" : \"Equity\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"Total Equity\" }, { \"value\" : \"-6805.98\" }] } }] }, \"type\" : \"Section\", \"group\" : \"TotalLiabilitiesAndEquity\", \"Summary\" : { \"ColData\" : [{ \"value\" : \"TOTAL LIABILITIES AND EQUITY\" }, { \"value\" : \"24742.44\" }] } }] }, \"Columns\" : { \"Column\" : [{ \"ColType\" : \"Account\", \"ColTitle\" : \"\", \"MetaData\" : [{ \"Name\" : \"ColKey\", \"Value\" : \"account\" }] }, { \"ColType\" : \"Money\", \"ColTitle\" : \"Total\", \"MetaData\" : [{ \"Name\" : \"ColKey\", \"Value\" : \"total\" }] }] } }");
然后您可以使用以下语法调出这些值。对于数组,您可以遍历值的数量。为了使其工作,您需要知道返回的值是什么 (I.E. Int, String, or Document) 。 BSON returns 值作为 "Object" 所以你必须转换值。例如:(字符串)bson.get("field")。这个JSON超级大。下面是一些如何在一行中解析的示例。
System.out.println(((String)((Document)root.get("Header")).get("ReportName")));
System.out.println(((String)((Document)((ArrayList)((Document)root.get("Header")).get("Option")).get(0)).get("Name")));
System.out.println(((String)((Document)((ArrayList)((Document)root.get("Header")).get("Option")).get(0)).get("Value")));
System.out.println(((String)((Document)root.get("Header")).get("DateMacro")));
System.out.println(((String)((Document)root.get("Header")).get("ReportBasis")));
System.out.println(((String)((Document)root.get("Header")).get("StartPeriod")));
System.out.println(((String)((Document)root.get("Header")).get("Currency")));
System.out.println(((String)((Document)root.get("Header")).get("EndPeriod")));
System.out.println(((String)((Document)root.get("Header")).get("Time")));
System.out.println(((String)((Document)root.get("Header")).get("SummarizeColumnsBy")));
System.out.println(((String)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)root.get("Rows")).get("Row")).get(0)).get("Header")).get("ColData")).get(0)).get("value")));
System.out.println(((String)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)root.get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Header")).get("ColData")).get(0)).get("value")));
System.out.println(((String)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)root.get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Header")).get("ColData")).get(0)).get("value")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)root.get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("ColData")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)((Document)((ArrayList)((Document)root.get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("Rows")).get("Row")).get(0)).get("ColData")).get(0)).get("value")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)root.get("Columns")).get("Column")).get(1)).get("MetaData")).get(0)).get("Name")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)root.get("Columns")).get("Column")).get(1)).get("MetaData")).get(0)).get("Value")));