Saya menggunakan json-simple untuk menempelkan beberapa data dari file json file tersebut terlihat seperti ini

{
  "steam": {
    "name": "steam",
    "location": "C:\\Program Files (x86)\\Steam"
  },
  "test": {
    "name": "test",
    "location": "test"
  },
  "fessseffes": {
    "name": "test",
    "location": "test2"
  },
  "dacdd": {
    "name": "dacdd",
    "location": "test"
  }
}

Saya ingin berinteraksi pada setiap objek "steam,test,fessseffes,dacdd" satu-satunya cara yang saya temukan adalah dengan .get("steam") tetapi itu tidak membantu karena saya ingin berinteraksi dengan semua objek dan menggunakannya untuk membuat objek Java .

0
thankarezos 8 Januari 2021, 07:20

3 jawaban

Jawaban Terbaik

Saya menggunakan gson alih-alih json perpustakaan sederhana. Kode itu berhasil.

try {
            // create Gson instance
            Gson gson = new Gson();

            // create a reader
            JsonReader reader = new JsonReader(new FileReader("src/data.json"));

            // convert JSON file to map
            Map<?, ?> map = gson.fromJson(reader, Map.class);
            
            
            for (Map.Entry<?, ?> entry : map.entrySet()) {
                Map<?, ?> mapi = (Map)entry.getValue();
                for (Map.Entry<?, ?> entryi : mapi.entrySet()) {
                    System.out.println(entryi.getKey() + "=" + entryi.getValue());
                }
            }
            // close reader
            reader.close();

        } catch (Exception ex) {
           ```
0
thankarezos 8 Januari 2021, 05:29

Anda dapat menggunakan lib pihak ketiga. Contoh: com.google.gson.JsonObject::entrySet() atau org.json.simple.JSONObject : JSONObject adalah HashMap.

1
Spinning 8 Januari 2021, 04:28

Bisakah Anda merujuk Pengantar Jackson ObjectMapper

Kode di bawah ini akan berfungsi untuk saat ini,

Contoh program dengan Jackson dan Gson (dengan bantuan POJO)

Dacdd.class

class Dacdd{
    public String name;
    public String location;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Dacdd{" +
                "name='" + name + '\'' +
                ", location='" + location + '\'' +
                '}';
    }
}

Kelas Fessseffes

class Fessseffes{
    public String name;
    public String location;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Fessseffes{" +
                "name='" + name + '\'' +
                ", location='" + location + '\'' +
                '}';
    }
}

Kelas Steam.

public class Steam{
    public String name;
    public String location;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Steam{" +
                "name='" + name + '\'' +
                ", location='" + location + '\'' +
                '}';
    }
}

Kelas uji

public class Test{
    public String name;
    public String location;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Test{" +
                "name='" + name + '\'' +
                ", location='" + location + '\'' +
                '}';
    }
}

Root.class

public class Root {
    public Steam steam;
    public Test test;
    public Fessseffes fessseffes;
    public Dacdd dacdd;

    public Steam getSteam() {
        return steam;
    }

    public void setSteam(Steam steam) {
        this.steam = steam;
    }

    @Override
    public String toString() {
        return "Root{" +
                "steam=" + steam +
                ", test=" + test +
                ", fessseffes=" + fessseffes +
                ", dacdd=" + dacdd +
                '}';
    }
}

data.json

{
  "steam": {
    "name": "steam",
    "location": "C:\\Program Files (x86)\\Steam"
  },
  "test": {
    "name": "test",
    "location": "test"
  },
  "fessseffes": {
    "name": "test",
    "location": "test2"
  },
  "dacdd": {
    "name": "dacdd",
    "location": "test"
  }
}

Main.class(Menggunakan Jackson)

    import com.fasterxml.jackson.databind.ObjectMapper;

    import java.io.IOException;
    public class Main {
    
        public static void main(String[]args) throws IOException {
      

    ObjectMapper objectMapper = new ObjectMapper();
    Root car = objectMapper.readValue(new FileReader("src/data.json"), Root.class);
    System.out.println(car);
    System.out.println(car.dacdd.name);
    System.out.println(car.steam.name);
    System.out.println(car.fessseffes.name);
    System.out.println(car.test.name);
    System.out.println(car.dacdd.location);
    System.out.println(car.steam.location);
    System.out.println(car.fessseffes.location);
    System.out.println(car.test.location);
    }
}

Kelas Utama (Menggunakan Gson)

import com.google.gson.Gson;

import java.io.IOException;

public class Main {

    public static void main(String[]args) throws IOException {
    Root car = new Gson().fromJson(new JsonReader(new FileReader("src/data.json")), Root.class);
    System.out.println(car);
    System.out.println(car.dacdd.name);
    System.out.println(car.steam.name);
    System.out.println(car.fessseffes.name);
    System.out.println(car.test.name);
    System.out.println(car.dacdd.location);
    System.out.println(car.steam.location);
    System.out.println(car.fessseffes.location);
    System.out.println(car.test.location);
    }
}

Keluaran:

 Root{steam=Steam{name='steam', location='C:\Program Files (x86)\Steam'}, test=Test{name='test', location='test'}, fessseffes=Fessseffes{name='test', location='test2'}, dacdd=Dacdd{name='dacdd', location='test'}}
dacdd
steam
test
test
test
C:\Program Files (x86)\Steam
test2
test
0
Sibin 8 Januari 2021, 06:00