我有一个非常简单的JSON文件,我需要从中获取字段名称和值。我在eclipse中使用Java 1.7和gson。这是我的JSON文件:
{
"header": [
{
"document_number": "document_number",
"report": "report",
"version": "version"
}
],
"summary": [
{
"row_type": "row_type",
"crs_id": "crs_id",
"report_begin": "report_begin",
"report_end": "report_end",
"gross_tax": "gross_tax",
"compensating_tax": "compensating_tax",
"withholding_tax": "withholding_tax",
"total_due_tax": "total_due_tax",
"penalty": "penalty",
"interest": "interest",
"total_due_due": "total_due_due"
}
],
"detail": [
{
"row_type": "row_type",
"county_name": "county_name",
"rate_type": "rate_type",
"location_code": "location_code",
"gross_receipts": "gross_receipts",
"deductions": "deductions",
"taxable_gross_receipts": "taxable_gross_receipts",
"tax_rate": "tax_rate","gross_tax": "gross_tax"
}
]
}
我需要知道不同的字段名称来导航文件。在我看来,我想加载JSON文件,然后返回第一个字段值(在本例中为标题)。然后返回该根下的字段名称和值。对不起,如果我的术语关闭,我是JSON的新手。我在网上找到了不同的gson文档,这一切都很糟糕。在不知道字段名称的情况下,我找不到任何可以提取值的东西。感谢
答案 0 :(得分:1)
这是代码。有关更多信息,请阅读内联评论或询问我。
创建一些POJO类,它们是JSON字符串的副本。
class MyJSON {
ArrayList<Header> header;
ArrayList<Summary> summary;
ArrayList<Detail> detail;
}
class Header {
String document_number;
String report;
String version;
}
// create classes for Summary and Detail as well in the similar way of Header
// the variable name must be same as it is in JSON string along with case.
...
// read JSON from the file
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(new File("resources/json1.txt")));
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
reader.close();
// create a new Gson object
Gson gson = new Gson();
// convert JSON string to POJO object
MyJSON object = gson.fromJson(builder.toString(), MyJSON.class);
// printing only header
for (Header header : object.header) {
System.out.println(header.document_number + ", " + header.report + ", "
+ header.version);
}
输出(仅打印标题)
document_number, report, version
答案 1 :(得分:0)
这是我准备好运行的解决方案。
package stackoverflow.questions;
import java.util.*;
import com.google.gson.*;
public class Q23704415 {
public static void main(String[] args) {
String json = "{ "+
" \"header\": [ "+
" { "+
" \"document_number\": \"document_number\", "+
" \"report\": \"report\", "+
" \"version\": \"version\" "+
" } "+
" ], "+
" "+
" \"summary\": [ "+
" { "+
" \"row_type\": \"row_type\", "+
" \"crs_id\": \"crs_id\", "+
" \"report_begin\": \"report_begin\", "+
" \"report_end\": \"report_end\", "+
" \"gross_tax\": \"gross_tax\", "+
" \"compensating_tax\": \"compensating_tax\", "+
" \"withholding_tax\": \"withholding_tax\", "+
" \"total_due_tax\": \"total_due_tax\", "+
" \"penalty\": \"penalty\", "+
" \"interest\": \"interest\", "+
" \"total_due_due\": \"total_due_due\" "+
" } "+
" ], "+
" "+
" \"detail\": [ "+
" "+
" { "+
" "+
" \"row_type\": \"row_type\", "+
" \"county_name\": \"county_name\", "+
" \"rate_type\": \"rate_type\", "+
" \"location_code\": \"location_code\", "+
" \"gross_receipts\": \"gross_receipts\", "+
" \"deductions\": \"deductions\", "+
" \"taxable_gross_receipts\": \"taxable_gross_receipts\", "+
" \"tax_rate\": \"tax_rate\",\"gross_tax\": \"gross_tax\" "+
" "+
" } "+
" "+
" ] "+
" "+
"} ";
JsonElement root = new JsonParser().parse(json);
JsonObject jo = root.getAsJsonObject();
JsonObject header = jo.get("header").getAsJsonArray().get(0).getAsJsonObject();
Set<Map.Entry<String, JsonElement>> set = header.entrySet();
for(Map.Entry<String, JsonElement> e : set){
System.out.println(e.getKey() + "->" + e.getValue());
}
}
}
关于你写的内容的一些注释。
header
是一个只包含一个JSON对象的数组,其余的JSON也是如此。如果你掌控JSON,你可以简化它header
下的字段,但它是一个数组,所以你必须首先解压缩并获得第一个位置我没有使用Gson
类,因为它通常在类层次结构中效果更好,就像Braj为您提供的那样。使用JsonParser,您可以更灵活地假设&#34;结构&#34;不会改变太多。也就是说,您有一个包含只有一个元素的数组的对象。
所以这一行
JsonObject header = jo.get("header").getAsJsonArray().get(0).getAsJsonObject();
基本上遍历您的JSON以获取正确的信息。我忘记了:这是我的代码的结果:
document_number->"document_number"
report->"report"
version->"version"