从元素列表中的 json 加载

发布于 2024-11-02 08:28:53 字数 7071 浏览 0 评论 0原文

我需要将这个 king json 文件加载到 Java 中的某个对象中。

[
    {
        "id": 1,
        "name": "FA1_M",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 1,
        "index_ring": 1,
        "order": 0
    },
    {
        "id": 2,
        "name": "A1_1_LED",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 1,
        "index_ring": 2,
        "order": 1
    },
    {
        "id": 3,
        "name": "FA1_A",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 2,
        "index_ring": 3,
        "order": 2
    },
    {
        "id": 4,
        "name": "A1_2_LED",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 2,
        "index_ring": 4,
        "order": 3
    },
    {
        "id": 5,
        "name": "FA1_BMA",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 3,
        "index_ring": 5,
        "order": 4
    },
    {
        "id": 6,
        "name": "A1_3_LED",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 3,
        "index_ring": 6,
        "order": 5
    },
    {
        "id": 7,
        "name": "Reserve_IN_4",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 4,
        "index_ring": 7,
        "order": 6
    },
    {
        "id": 8,
        "name": "A1_4_LED",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 4,
        "index_ring": 8,
        "order": 7
    },
    {
        "id": 9,
        "name": "Reserve_IN_5",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 5,
        "index_ring": 9,
        "order": 8
    },
    {
        "id": 10,
        "name": "A1_5_LED",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 5,
        "index_ring": 10,
        "order": 9
    },
    {
        "id": 11,
        "name": "Reserve_IN_6",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 6,
        "index_ring": 11,
        "order": 10
    },
    {
        "id": 12,
        "name": "Reserve_OUT_6",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 6,
        "index_ring": 12,
        "order": 11
    },
    {
        "id": 13,
        "name": "Reserve_IN_7",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 7,
        "index_ring": 13,
        "order": 12
    },
    {
        "id": 14,
        "name": "KK1_ENB",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 7,
        "index_ring": 14,
        "order": 13
    },
    {
        "id": 15,
        "name": "FA2_BMA",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 8,
        "index_ring": 15,
        "order": 14
    },
    {
        "id": 16,
        "name": "KK2_ENB",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 8,
        "index_ring": 16,
        "order": 15
    },
    {
        "id": 17,
        "name": "Smoke1_1",
        "ring_number": 1,
        "type": 2,
        "unit_index": 2,
        "device_type": 2,
        "address": 1,
        "index_ring": 17,
        "order": 16
    },
    {
        "id": 18,
        "name": "Smoke1_2",
        "ring_number": 1,
        "type": 2,
        "unit_index": 3,
        "device_type": 12,
        "address": 1,
        "index_ring": 18,
        "order": 17
    },
    {
        "id": 19,
        "name": "Smoke1_3",
        "ring_number": 1,
        "type": 2,
        "unit_index": 4,
        "device_type": 2,
        "address": 1,
        "index_ring": 19,
        "order": 18
    },
    {
        "id": 20,
        "name": "Smoke1_4",
        "ring_number": 1,
        "type": 2,
        "unit_index": 5,
        "device_type": 2,
        "address": 1,
        "index_ring": 20,
        "order": 19
    },
    {
        "id": 21,
        "name": "Fire1_1",
        "ring_number": 1,
        "type": 1,
        "unit_index": 6,
        "device_type": 4,
        "address": 1,
        "index_ring": 21,
        "order": 20
    },
    {
        "id": 22,
        "name": "Fire1_2",
        "ring_number": 1,
        "type": 1,
        "unit_index": 6,
        "device_type": 4,
        "address": 2,
        "index_ring": 22,
        "order": 21
    },
    {
        "id": 23,
        "name": "Fire1_3",
        "ring_number": 1,
        "type": 1,
        "unit_index": 7,
        "device_type": 1,
        "address": 1,
        "index_ring": 23,
        "order": 22
    },
    {
        "id": 24,
        "name": "Motorless1",
        "ring_number": 1,
        "type": 5,
        "unit_index": 8,
        "device_type": 13,
        "address": 1,
        "index_ring": 24,
        "order": 23
    },
    {
        "id": 25,
        "name": "Fire1_4",
        "ring_number": 1,
        "type": 1,
        "unit_index": 9,
        "device_type": 14,
        "address": 1,
        "index_ring": 25,
        "order": 24
    },
    {
        "id": 26,
        "name": "Fire1_5",
        "ring_number": 1,
        "type": 1,
        "unit_index": 9,
        "device_type": 14,
        "address": 2,
        "index_ring": 26,
        "order": 25
    }
]

我有类 Element 描述一个元素

public class element {
    protected int id;
    protected int type;
    protected String state;
    protected int status;
    protected int device_type;
    private Map<String, Integer>mask;


    public int getId(){return this.id;}
    public void setId(int id){this.id=id;}
    public int getType(){return this.type;}
    public void setType(int type){this.type=type;}
    public String getState(){return this.state;}
    public void setState(String state){this.state=state;}
    public int getStatus(){return this.status;}
    public void setStatus(int status){this.status=status;}
    public int getDeviceType(){return this.device_type;}
    public void setDeviceType(int device_type){this.device_type=device_type;}
    public Map<String,Integer> getMask(){return this.mask;}
    public void setMask(Map<String,Integer> mask){this.mask=mask;}

    //index start by 1, not 0
    public boolean getBit(int index){
        return ((this.status>>(index-1)) & 0x1)==1;
    }

    public ElementView generateView(){
        return null;
    }

}

如何在元素列表中加载 json?我用 GSON 查看了 stackoverflow 问题,但是需要在 json 中为这个数组指定一些名称,但我没有。有人可以帮忙吗?

I need to load this king json file into some object in Java.

[
    {
        "id": 1,
        "name": "FA1_M",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 1,
        "index_ring": 1,
        "order": 0
    },
    {
        "id": 2,
        "name": "A1_1_LED",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 1,
        "index_ring": 2,
        "order": 1
    },
    {
        "id": 3,
        "name": "FA1_A",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 2,
        "index_ring": 3,
        "order": 2
    },
    {
        "id": 4,
        "name": "A1_2_LED",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 2,
        "index_ring": 4,
        "order": 3
    },
    {
        "id": 5,
        "name": "FA1_BMA",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 3,
        "index_ring": 5,
        "order": 4
    },
    {
        "id": 6,
        "name": "A1_3_LED",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 3,
        "index_ring": 6,
        "order": 5
    },
    {
        "id": 7,
        "name": "Reserve_IN_4",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 4,
        "index_ring": 7,
        "order": 6
    },
    {
        "id": 8,
        "name": "A1_4_LED",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 4,
        "index_ring": 8,
        "order": 7
    },
    {
        "id": 9,
        "name": "Reserve_IN_5",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 5,
        "index_ring": 9,
        "order": 8
    },
    {
        "id": 10,
        "name": "A1_5_LED",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 5,
        "index_ring": 10,
        "order": 9
    },
    {
        "id": 11,
        "name": "Reserve_IN_6",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 6,
        "index_ring": 11,
        "order": 10
    },
    {
        "id": 12,
        "name": "Reserve_OUT_6",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 6,
        "index_ring": 12,
        "order": 11
    },
    {
        "id": 13,
        "name": "Reserve_IN_7",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 7,
        "index_ring": 13,
        "order": 12
    },
    {
        "id": 14,
        "name": "KK1_ENB",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 7,
        "index_ring": 14,
        "order": 13
    },
    {
        "id": 15,
        "name": "FA2_BMA",
        "ring_number": 1,
        "type": 3,
        "unit_index": 1,
        "device_type": 3,
        "address": 8,
        "index_ring": 15,
        "order": 14
    },
    {
        "id": 16,
        "name": "KK2_ENB",
        "ring_number": 1,
        "type": 4,
        "unit_index": 1,
        "device_type": 3,
        "address": 8,
        "index_ring": 16,
        "order": 15
    },
    {
        "id": 17,
        "name": "Smoke1_1",
        "ring_number": 1,
        "type": 2,
        "unit_index": 2,
        "device_type": 2,
        "address": 1,
        "index_ring": 17,
        "order": 16
    },
    {
        "id": 18,
        "name": "Smoke1_2",
        "ring_number": 1,
        "type": 2,
        "unit_index": 3,
        "device_type": 12,
        "address": 1,
        "index_ring": 18,
        "order": 17
    },
    {
        "id": 19,
        "name": "Smoke1_3",
        "ring_number": 1,
        "type": 2,
        "unit_index": 4,
        "device_type": 2,
        "address": 1,
        "index_ring": 19,
        "order": 18
    },
    {
        "id": 20,
        "name": "Smoke1_4",
        "ring_number": 1,
        "type": 2,
        "unit_index": 5,
        "device_type": 2,
        "address": 1,
        "index_ring": 20,
        "order": 19
    },
    {
        "id": 21,
        "name": "Fire1_1",
        "ring_number": 1,
        "type": 1,
        "unit_index": 6,
        "device_type": 4,
        "address": 1,
        "index_ring": 21,
        "order": 20
    },
    {
        "id": 22,
        "name": "Fire1_2",
        "ring_number": 1,
        "type": 1,
        "unit_index": 6,
        "device_type": 4,
        "address": 2,
        "index_ring": 22,
        "order": 21
    },
    {
        "id": 23,
        "name": "Fire1_3",
        "ring_number": 1,
        "type": 1,
        "unit_index": 7,
        "device_type": 1,
        "address": 1,
        "index_ring": 23,
        "order": 22
    },
    {
        "id": 24,
        "name": "Motorless1",
        "ring_number": 1,
        "type": 5,
        "unit_index": 8,
        "device_type": 13,
        "address": 1,
        "index_ring": 24,
        "order": 23
    },
    {
        "id": 25,
        "name": "Fire1_4",
        "ring_number": 1,
        "type": 1,
        "unit_index": 9,
        "device_type": 14,
        "address": 1,
        "index_ring": 25,
        "order": 24
    },
    {
        "id": 26,
        "name": "Fire1_5",
        "ring_number": 1,
        "type": 1,
        "unit_index": 9,
        "device_type": 14,
        "address": 2,
        "index_ring": 26,
        "order": 25
    }
]

I have class Element which describe one element

public class element {
    protected int id;
    protected int type;
    protected String state;
    protected int status;
    protected int device_type;
    private Map<String, Integer>mask;


    public int getId(){return this.id;}
    public void setId(int id){this.id=id;}
    public int getType(){return this.type;}
    public void setType(int type){this.type=type;}
    public String getState(){return this.state;}
    public void setState(String state){this.state=state;}
    public int getStatus(){return this.status;}
    public void setStatus(int status){this.status=status;}
    public int getDeviceType(){return this.device_type;}
    public void setDeviceType(int device_type){this.device_type=device_type;}
    public Map<String,Integer> getMask(){return this.mask;}
    public void setMask(Map<String,Integer> mask){this.mask=mask;}

    //index start by 1, not 0
    public boolean getBit(int index){
        return ((this.status>>(index-1)) & 0x1)==1;
    }

    public ElementView generateView(){
        return null;
    }

}

How to load json in list of elements ? I looked on stackoverflow questoion with GSON, but there need to have some name this array in json and I don't have that. Can anybody help ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热情消退 2024-11-09 08:28:53

您需要指定元素类型/类。例如,你有下面的数组 json
[{"id":"01","name":"Joe"},{"id":"02","name":"Doe"}]

您必须创建学生班级

public class Student {
    private String id;
    private  String name;

    public Student() {

    }

    public Student(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

}

之后需要指定集合类型,并访问fromJson方法

Type collectionType = new TypeToken<List<Student>>(){}.getType();
List<Student> students = gson.fromJson(json, collectionType);

整个测试代码如下

public class When_serialize_list_object {
    private List<Student> _students;
    private Gson gson = new Gson();

    @Before
    public void setUp() {
        _students = new ArrayList<Student>();

        Student student1 = new Student("01", "Joe");
        Student student2 = new Student("02", "Doe");

        _students.add(student1);
        _students.add(student2);
    }

    @Test
    public void should_able_to_deserialize() {      
        String json = gson.toJson(_students);

        System.out.println(json);
        Type collectionType = new TypeToken<List<Student>>(){}.getType();
        List<Student> students = gson.fromJson(json, collectionType);

        Assert.assertEquals(students, _students);
        System.out.println(students);
    }

}

You need to specify your element types/classes. for instance, you have array json bellow
[{"id":"01","name":"Joe"},{"id":"02","name":"Doe"}]

You must to create student class

public class Student {
    private String id;
    private  String name;

    public Student() {

    }

    public Student(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

}

After that you need to specify collection type, and access fromJson method

Type collectionType = new TypeToken<List<Student>>(){}.getType();
List<Student> students = gson.fromJson(json, collectionType);

entire test code as follows

public class When_serialize_list_object {
    private List<Student> _students;
    private Gson gson = new Gson();

    @Before
    public void setUp() {
        _students = new ArrayList<Student>();

        Student student1 = new Student("01", "Joe");
        Student student2 = new Student("02", "Doe");

        _students.add(student1);
        _students.add(student2);
    }

    @Test
    public void should_able_to_deserialize() {      
        String json = gson.toJson(_students);

        System.out.println(json);
        Type collectionType = new TypeToken<List<Student>>(){}.getType();
        List<Student> students = gson.fromJson(json, collectionType);

        Assert.assertEquals(students, _students);
        System.out.println(students);
    }

}
葵雨 2024-11-09 08:28:53

例如,使用 JSON-Simple

  Object obj=JSONValue.parse(jsonString);
  JSONArray array=(JSONArray)obj;
  JSONObject element0=(JSONObject)array.get(0);

  Element el = new Element();
  el.setId( element0.get("id") );
  ...

Using JSON-Simple library, for instance

  Object obj=JSONValue.parse(jsonString);
  JSONArray array=(JSONArray)obj;
  JSONObject element0=(JSONObject)array.get(0);

  Element el = new Element();
  el.setId( element0.get("id") );
  ...
揪着可爱 2024-11-09 08:28:53

看看杰克逊。它在解析 JSON 方面比 GSON 更有效。我通常先伟大我的对象,然后使用 Jackson 将其发送到 Json 字符串,这样我就知道我的 Json 字符串需要是什么样子。这是一个示例:

User user = new User();
        user.setFirstName("Kmb");
        ....
        UserStatus userStatus = new UserStatus();
        userStatus.setKey("1");
        userStatus.setKey("active");
        user.setUserStatus(userStatus);

        String strUser="";
        try {
            strUser =mapper.writeValueAsString(user);
        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       System.out.println(strUser);

一旦您了解了字符串应如何格式化,请调整您的输入以匹配 Jackson 想要的格式。然后将字符串与您的类一起传递到映射器中。

try {
        String str; //your string here
           User user2 = mapper.readValue(str, User.class);
           System.out.println(user2.getFirstName());
           System.out.println(user2.getServiceType().getValue());

        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

如果您想要一个元素数组,您可能需要创建一个简单的类来包装元素列表并通过 Jackson 运行它。

  <dependencies>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.7.5</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.7.5</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
  </dependencies>

Take a look at Jackson. It is more effecient than GSON at parcing JSON. I ussually great my object first and send it to a Json String using Jackson so I know what my Json String needs to look like. Here is an example:

User user = new User();
        user.setFirstName("Kmb");
        ....
        UserStatus userStatus = new UserStatus();
        userStatus.setKey("1");
        userStatus.setKey("active");
        user.setUserStatus(userStatus);

        String strUser="";
        try {
            strUser =mapper.writeValueAsString(user);
        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       System.out.println(strUser);

Once you see how the string should be formatted adjust your input to match the format Jackson would like. Then pass the string into the mapper along with your class.

try {
        String str; //your string here
           User user2 = mapper.readValue(str, User.class);
           System.out.println(user2.getFirstName());
           System.out.println(user2.getServiceType().getValue());

        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

If you want an array of your elements you may need to create a simple class that wraps a list of elements and run that through Jackson.

  <dependencies>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.7.5</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.7.5</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
  </dependencies>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文