使用Java从Json读取对象(gson)

niknxzdl  于 2022-11-06  发布在  Java
关注(0)|答案(1)|浏览(191)

我在阅读Json文件中的对象时遇到问题。我已经尝试将其视为ArrayList和Object,但我不知道还能尝试什么,因为它返回null。如何从对象中取出项?
进口

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.*;

Json文件

{
    "letters": 4,
    "numbers": 4,
    "positions": 4,
    "id":[[-1,0],[0,1],[1,0],[0,-1]],
    "wall":{
        "(0,0)":{"value":0,"walls": [false,true,true,false]},
        "(1,2)":{"value":0,"walls": [false,false,false,true]},
        "(5,7)":{"value":0,"walls": [false,true,false,false]},
        "(0,3)":{"value":0,"walls": [false,false,true,true]},
        "(2,8)":{"value":0,"walls": [true,false,true,false]}
    }
}

Java程式码

public static void readJson(String path) {

        Gson gson = new Gson();
        String file_gson = "";

        try(BufferedReader br = new BufferedReader(new FileReader(path))){
            String line;
            while((line = br.readLine()) != null) {
                file_gson += line;  
            }
            Fich element = gson.fromJson(file_gson, Fich.class);
            System.out.println(element.letters);

        }catch(FileNotFoundException ex) {
            System.out.println("");
        }catch(IOException ex) {
            System.out.println("");
        }
    }

但是在对象类(Walls)中,它不会读给我听,如果我把(Walls)作为字符串,它会给我一个错误。

public class Fich{
    public int letters;
    public int numbers;
    public int positions;
    public ArrayList id;
    public Walls walls;

    public Fich(int letters, int numbers, Walls walls){
        this.letters = letters;
        this.letters = letters;
    }
}
epfja78i

epfja78i1#

id字段是包含另一个JSON Array-s的JSON Array。您可以将其Map到List<List<Integer>>wall字段是JSON Object,其中键是String-s,值是另一个JSON Object-s。您可以将其Map到Map<String, Wall>
请参见以下示例:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

public class GsonApp {

    public static void main(String[] args) throws IOException {
        Path pathToJson = Paths.get("./resource/test.json");
        Gson gson = new GsonBuilder().create();
        try (BufferedReader reader = Files.newBufferedReader(pathToJson)) {
            Fich fich = gson.fromJson(reader, Fich.class);
            System.out.println(fich);
        }
    }
}

class Fich {
    private int letters;
    private int numbers;
    private int positions;
    private List<List<Integer>> id;
    private Map<String, Wall> wall;

    // getters, setters, toString
}

class Wall {
    private int value;
    private List<Boolean> walls;

    // getters, setters, toString
}

以上代码打印:

Fich[letters=4, numbers=4, positions=4, id=[[-1, 0], [0, 1], [1, 0], [0, -1]], wall={(0,0)=Wall[value=0, walls=[false, true, true, false]], (1,2)=Wall[value=0, walls=[false, false, false, true]], (5,7)=Wall[value=0, walls=[false, true, false, false]], (0,3)=Wall[value=0, walls=[false, false, true, true]], (2,8)=Wall[value=0, walls=[true, false, true, false]]}]

相关问题