Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

java - Convert json to Object List

I have the following String:

String json = "[{"id": "0", "ip": "123", "mac": "456"}, {"id": "1", "ip": "111", "mac": "222"}]";

And a SlaveEntity Entity that has:

public class SlaveEntity extends BaseEntity {

    private String ip;
    private String macAddress;
    private String status;

    @OneToMany(mappedBy="slave", targetEntity = PositionEntity.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<PositionEntity> positions;

}

I am writing a method that takes the json and returns a List of SlaveEntity:

public static List<SlaveEntity> JsonToSlaveEntity(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        List<SlaveEntity> obj = new ArrayList<SlaveEntity>();

        try {
           obj = objectMapper.readValue(json, List.class);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return obj;
    }

The problem is that the obj List results like this:

enter image description here

But what I need the obj List to be is like this:

enter image description here

So how can I get the needed list?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can convert the result to an object list, or you can pass in a type parameter rather than the List class.

String jsonString = "[{"id": "0", "ip": "123", "mac": "456"}, {"id": "1", "ip": "111", "mac": "222"}]";

With Object

List<Object> items = objectMapper.readValue(
    jsonString,
    objectMapper.getTypeFactory().constructParametricType(List.class, Object.class)
);

With SlaveEntity

List<SlaveEntity> items = objectMapper.readValue(
    jsonString,
    objectMapper.getTypeFactory().constructCollectionType(List.class, SlaveEntity.class)
);

Update

This is what I have come up with, and it works.

EntityTest

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;

public class EntityTest {
    public static void main(String[] args) {
        String json = "[{"id": "0", "ip": "123", "mac": "456"}, {"id": "1", "ip": "111", "mac": "222"}]";

        for (SlaveEntity entity : jsonToSlaveEntity(json)) {
            System.out.println(entity);
        }
    }

    public static List<SlaveEntity> jsonToSlaveEntity(String json) {
        ObjectMapper objectMapper = new ObjectMapper();

        try {
           return objectMapper.readValue(
                   json,
                objectMapper.getTypeFactory().constructCollectionType(List.class, SlaveEntity.class)
            );

        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ArrayList<SlaveEntity>();
    }
}

BaseEntity

public class BaseEntity {
    private long id;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}

SlaveEntity

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;

import com.fasterxml.jackson.annotation.JsonProperty;

public class SlaveEntity extends BaseEntity {
    private String ip;

    @JsonProperty("mac")
    private String macAddress;

    private String status;

    @OneToMany(mappedBy = "slave", targetEntity = PositionEntity.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<PositionEntity> positions;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getMacAddress() {
        return macAddress;
    }

    public void setMacAddress(String macAddress) {
        this.macAddress = macAddress;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<PositionEntity> getPositions() {
        return positions;
    }

    public void setPositions(List<PositionEntity> positions) {
        this.positions = positions;
    }

    @Override
    public String toString() {
        return String.format(
                "SlaveEntity [id=%d, ip=%s, mac=%s, status=%s, positions=%s]",
                getId(), ip, macAddress, status, positions);
    }
}

PositionEntity

public class PositionEntity {
    // ?
}

Result

SlaveEntity [id=0, ip=123, mac=456, status=null, positions=null]
SlaveEntity [id=1, ip=111, mac=222, status=null, positions=null]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...