java

bkhjykvo  于 2021-06-27  发布在  Java
关注(0)|答案(3)|浏览(405)

我正在使用json simple从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"
  }
}

我想在每个对象上交互“steam,test,fessseffes,dacdd”,我找到的唯一方法是使用.get(“steam”),但这没有帮助,因为我想交互所有对象并使用它们来创建java对象。

ego6inou

ego6inou1#

你可以使用第三方库。例如:com.google.gson.jsonobject::entryset()或org.json.simple.jsonobject:jsonobject是一个hashmap。

fykwrbwg

fykwrbwg2#

你能介绍一下jackson objectmapper吗
下面的代码暂时有效,
jackson和gson的示例程序(在pojos的帮助下)
dacdd类

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 + '\'' +
                '}';
    }
}

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 + '\'' +
                '}';
    }
}

蒸汽等级

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 + '\'' +
                '}';
    }
}

测试等级

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 + '\'' +
                '}';
    }
}

根目录类

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 +
                '}';
    }
}

数据.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(使用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);
    }
}

主类(使用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);
    }
}

输出:

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
kg7wmglp

kg7wmglp3#

我使用gson而不是json简单库。那个密码成功了。

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) {
           ```

相关问题