我一直在尝试使用JSON
读取一些简单的Gson
文件。这是我的JSON
:
[
{
"OBJECTID": 14476381,
"ROW_": null,
"LOCATION": "1400 BLK S CAPITOL ST SE N/B",
"ADDRESS_ID": 277954,
"STREETSEGID": null,
"XCOORD": 399257.7,
"YCOORD": 133847.29,
"TICKETTYPE": "Moving",
"FINEAMT": 300,
"TOTALPAID": 0,
"PENALTY1": 300,
"PENALTY2": null,
"ACCIDENTINDICATOR": "No",
"TICKETISSUEDATE": "2018-04-01T11:59:00.000Z",
"VIOLATIONCODE": "T122",
"VIOLATIONDESC": "SPEED 26-30 MPH OVER THE SPEED LIMIT",
"ROW_ID": null
},
{
"OBJECTID": 14476382,
"ROW_": null,
"LOCATION": "100 BLK MICHIGAN AVE NW E/B",
"ADDRESS_ID": 815694,
"STREETSEGID": 1405,
"XCOORD": 398728.3,
"YCOORD": 139835.9,
"TICKETTYPE": "Moving",
"FINEAMT": 100,
"TOTALPAID": 0,
"PENALTY1": 100,
"PENALTY2": null,
"ACCIDENTINDICATOR": "No",
"TICKETISSUEDATE": "2018-04-01T00:35:00.000Z",
"VIOLATIONCODE": "T119",
"VIOLATIONDESC": "SPEED 11-15 MPH OVER THE SPEED LIMIT",
"ROW_ID": null
},
...
]
这是我到目前为止在代码中所拥有的:
private void readJson(String[] months) throws NumberFormatException, IOException {
for (int i = 0; i < months.length; i++) {
String month = months[i];
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(month));
JsonReader reader = new JsonReader(isr);
reader.beginArray();
Gson gson = new GsonBuilder().create();
while (reader.hasNext()) {
VOMovingViolations violation = gson.fromJson(reader, VOMovingViolations.class);
data.push(violation);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我的VOMovingViolation课
class VOMovingViolation {
private int objectId;
private byte row;
private String location;
private int addressId;
private byte streetSegId;
private double xCord;
private double yCord;
private String ticketType;
private short fineamt;
private short totalPaid;
private short penalty1;
private short penalty2;
private String accidentIndicator;
private String ticketIssueDate;
private String violationCode;
private String violationDescription;
private byte rowId;
public VOMovingViolations(int objectId, byte row, String location, int addressId, byte streetSegId, double xCord,
double yCord, String ticketType, short fineamt, short totalPaid, short penalty1, short penalty2,
String accidentIndicator, String ticketIssueDate, String violationCode, String violationDescription,
byte rowId) {
this.objectId = objectId;
this.row = row;
this.location = location;
this.addressId = addressId;
this.streetSegId = streetSegId;
this.xCord = xCord;
this.yCord = yCord;
this.ticketType = ticketType;
this.fineamt = fineamt;
this.totalPaid = totalPaid;
this.penalty1 = penalty1;
this.penalty2 = penalty2;
this.accidentIndicator = accidentIndicator;
this.ticketIssueDate = ticketIssueDate;
this.violationCode = violationCode;
this.violationDescription = violationDescription;
}
@Override
public int compareTo(VOMovingViolations o) {
return Integer.compare(objectId, o.objectId);
}
}
我需要为从JSON object
中读取的每个JSON
创建一个对象,然后将其添加到我创建的堆栈中。如果我实现了已有的功能并在控制台中打印了我的第一个违规行为,我会得到
ObjectID:0 ||地址ID:0 ||街段ID:0 ||总付款:0 ||位置:无||票证发行日期:无||事故指示器:空||违规描述:空
如何正确解析?
答案 0 :(得分:0)
JSON
中的名称不适合POJO
中的属性名称。您需要提供额外的命名约定或使用SerializedName
注释。按照命名约定,它可能如下所示:
import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.List;
public class GsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
Gson gson = new GsonBuilder()
.setFieldNamingStrategy(new FieldNamingStrategy() {
@Override
public String translateName(Field f) {
final String name = f.getName();
switch (name) {
case "row":
return "ROW_";
case "rowId":
return "ROW_ID";
case "violationDescription":
return "VIOLATIONDESC";
default:
return f.getName().toUpperCase();
}
}
})
.create();
Type type = new TypeToken<List<VOMovingViolations>>() {
}.getType();
List<VOMovingViolations> violations = gson.fromJson(new FileReader(jsonFile), type);
violations.forEach(System.out::println);
}
}
如果要使用SerializedName
批注,则需要如下所示对POJO
中的每个字段进行批注:
@SerializedName("OBJECTID")
private int objectId;
@SerializedName("ROW_")
private byte row;
...
在这种情况下,您不需要注册FieldNamingStrategy
对象,并且您的Gson
对象看起来像:
Gson gson = new GsonBuilder().create();
此外,请勿在{{1}}循环中创建Gson
。在循环之前创建一次就足够了。