我在将JSON文件中的数据“转换”为所需的Java对象时遇到问题。 JSON文件如下所示:
{
"initialInventory": [
{"bookTitle": "Harry Poter", "amount": 10, "price": 90},
{"bookTitle": "The Hunger Games", "amount": 90, "price": 102}
],
"initialResources":[
{"vehicles":[
{"license":123483, "speed": 2},
{"license":999994, "speed": 4}
]
}
],
"services":{
"time": {
"speed": 1000,
"duration": 24
},
"selling": 6,
"inventoryService": 3,
"logistics": 4,
"resourcesService": 2,
"customers": [
{
"id": 123456789,
"name": "Bruria",
"address": "NewYork 123",
"distance":33,
"creditCard":{"number":67890,"amount":88},
"orderSchedule": [
{"bookTitle": "Harry Poter", "tick": 3},
{"bookTitle": "The Hunger Games", "tick": 3}
]
},
{
"id": 234567891,
"name": "Shraga",
"address": "BeerSheva 3333",
"distance":12,
"creditCard":{"number":453536,"amount":220},
"orderSchedule": [
{"bookTitle": "The Hunger Games", "tick": 12}
]
}
]
}
我现在将要处理的所有JSON文件都具有相同的结构。 我尝试使用Gson解析文件(为此需要使用Gson), 我搜索了很多有关此问题的视频和教程,但其中大多数都过于简化或根本不相关,感觉或者有太多的方法可以做,或者我只是不明白该怎么做正确。
这是我到目前为止尝试过的:
public class BookStoreRunner {
public static void main(String[] args) throws IOException { //has to be added, we get an error without it. can be dealt with try-catch, doesn't matter.
Gson gson=new Gson();
File jsonfile= new File("pathtofile"); //should be the actual path to the file.
InputStream iStream= new FileInputStream(jsonfile);
Reader reader = new InputStreamReader(iStream);
JsonReader jsonReader =gson.newJsonReader(reader);
JsonParser parser= new JsonParser();
BookInventoryInfo[] books=gson.fromJson("initialInventory",BookInventoryInfo[].class);
ResourcesHolder.getInstance().load(gson.fromJson("vehicles",DeliveryVehicle[].class));
int timespeed= gson.fromJson("speed",int.class);
int timeduration=gson.fromJson("duration",int.class);
int numOfSellers=gson.fromJson("selling",int.class);
int numOfInventories=gson.fromJson("inventoryService",int.class);
int numOfLogistics=gson.fromJson("logistics",int.class);
int numOfResourceServices=gson.fromJson("resourcesService",int.class);
Customer[] customers=gson.fromJson("customers",Customer[].class);
/* JsonElement jElement = parser.parse(reader); // JsonElement is equivalent to a list in java
JsonObject jObject = jElement.getAsJsonObject(); //equivalent to an object of a class that implements the 'map' interface, e.g contains pairs of key:value
JsonObject initialInventory = jObject.getAsJsonObject("initialInventory");
JsonArray inventory =initialInventory.getAsJsonArray(); //it is an array (json syntax), but a JSONArray is not iterable.
// dealing with inventory
int i=0; //counter for all the books to be inserted to inventory array (index)
for (Map.Entry<String,JsonElement> entry: initialInventory.entrySet()) //.entrySet() gives a set view of the JSONObject, which is iterable
{
}
JsonArray iResources= jObject.getAsJsonArray("initialResources");
JsonObject veh = iResources.get(0).getAsJsonObject();
//initialization of java object, for the initialResources
JsonObject services=jObject.getAsJsonObject("services");
JsonObject timedata=jObject.getAsJsonObject("services").getAsJsonObject("time");
int numOfSelling= gson.fromJson(jObject.getAsJsonObject("services").getAsJsonPrimitive("selling"), int.class);
int numOfInventories= gson.fromJson(jObject.getAsJsonObject("services").getAsJsonPrimitive("inventoryService"), int.class);
int numOfIogistics=gson.fromJson(jObject.getAsJsonObject("services").getAsJsonPrimitive("logistics"),int.class);
int numOfResourceServices= gson.fromJson(jObject.getAsJsonObject("services").getAsJsonPrimitive("resourcesService"),int.class);
JsonArray jsonCustomers = jObject.getAsJsonObject("services").getAsJsonArray("customers");
JsonObject jsonCustomersObject=jObject.getAsJsonObject("services").getAsJsonObject("customers");
Customer[] customers = new Customer[5000]; // check if there's a problem with this part. unsure how many customers there are, or if we need\can use a list.
int i=0; //customers index
for (Map.Entry<String,JsonElement> entry: jsonCustomersObject.entrySet())
{ //assuming we will implement a constructor for Customer which receives all the required values
}
*/
我尝试用另一种方法解析文件,在代码末尾将其标记为注释。 我无法弄清楚问题是否出在我对JSON的一般理解,我对Java I \ O用法的了解还是我只是缺少一些东西。
如何使用Gson有效地从JSON文件创建所需的对象?