5

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?

0

1 Answer 1

6

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]
Sign up to request clarification or add additional context in comments.

5 Comments

Tried the solution 'With SlaveEntity' provided but now I get this error: com.fasterxml.jackson.databind.JsonMappingException: Multiple back-reference properties with name 'defaultReference' at [Source: [{"id": "0", "ip": "123", "mac": "456"}, {"id": "1", "ip": "111", "mac": "222"}]; line: 1, column: 1]
The solution 'With Object' gives me the exact results provided initially in this question.
Do you have more than three fields that you are not telling us about? Please post the actual SlaveEntity class, if you can.
@Cristian: I updated the question. This works for me -- if you have any other issues, they might be in your other classes eg. BaseEntity, PositionEntity, ...
so @Mr.Polywhirl you are right. There was a problem in PositionEntity and that is the reason for which your method gave me error. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.