How to Use Jackson to Deserialise an Array of Objects

How to use Jackson to deserialise an array of objects

First create a mapper :

import com.fasterxml.jackson.databind.ObjectMapper;// in play 2.3
ObjectMapper mapper = new ObjectMapper();

As Array:

MyClass[] myObjects = mapper.readValue(json, MyClass[].class);

As List:

List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){});

Another way to specify the List type:

List<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));

Jackson Deserializing an array of objects into an array list

Note that leading zeroes are not allowed, so this JSON will be invalid. We can use JSONLint to check this.

Also, ObjectMapper throws com.fasterxml.jackson.databind.JsonMappingException: Invalid numeric value: Leading zeroes not allowed.

After removing leading zeroes, you can do:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

Rows rowsArrayList = mapper.readValue(new File(ClientsRecordsPath), Rows.class);

Output:

JSON array to Array objects...
Row(userId=1, commission=0.0, swaps=-1.87, profit=-73.39, comment=MAM|12345678|10020031)
Row(userId=2, commission=0.0, swaps=0.0, profit=12.23, comment=PAMM|12345678|10229501)
Row(userId=3, commission=0.0, swaps=0.0, profit=396.77, comment=PAMM|12345678|10229501)

While that works fine, I suggest to remove the wrapper class if it has no use.
We can deserialize straight to a List<Row>:

List<Row> rows = Arrays.asList(mapper.treeToValue(mapper.readTree(new File(ClientsRecordsPath)).get("rows"), Row[].class));
rows.forEach(System.out::println);

Row(userId=1, commission=0.0, swaps=-1.87, profit=-73.39, comment=MAM|12345678|10020031)
Row(userId=2, commission=0.0, swaps=0.0, profit=12.23, comment=PAMM|12345678|10229501)
Row(userId=3, commission=0.0, swaps=0.0, profit=396.77, comment=PAMM|12345678|10229501)

How to use Jackson to deserialize a JSON Array into java objects

Your bean class should be like below:-

 import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;
public class InList {
@JsonProperty("inList")
private List<InListDetail> inList;
public List<InListDetail> getInList() {
return inList;
}
public void setInList(List<InListDetail> inList) {
this.inList = inList;
}
}

import org.codehaus.jackson.annotate.JsonProperty;
public class InListDetail {
@JsonProperty("cmd")
private String cmd;
@JsonProperty("name")
private String name;
@JsonProperty("pri")
private int pri;
public String getCmd() {
return cmd;
}
public void setCmd(String cmd) {
this.cmd = cmd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public int getPri() {
return pri;
}

public void setPri(int pri) {
this.pri = pri;
}

}

How to deserialize an array of objects using Jackson or any other api?

First off, I'm assuming that

"Labels": {
},

should actually be

"Labels": [
],

since you've defined it to be a list of Strings and not an object of it's own.

This code works for me:

Port.java:

public class Port {
@JsonProperty("IP")
private String ip;

@JsonProperty("PrivatePort")
private int privatePort;

@JsonProperty("PublicPort")
private int publicPort;

@JsonProperty("Type")
private String type;

// Getters and setters
}

main():

String json = ... // Your JSON with the above correction
ObjectMapper objectMapper = new ObjectMapper();
List<Container> containers = objectMapper.readValue(json, new TypeReference<List<Container>>() {});
System.out.println(containers.size());
for(Container container : containers) {
System.out.println("Container Id : " + container.getId());
}

2

Container Id : c00afc1ae5787282fd92b3dde748d203a83308d18aaa566741bef7624798af10

Container Id : d70f439231d99889c1a8e96148f13a77cb9b83ecf8c9d4c691ddefa40286c04c

jackson deserialize array into a java object

Add following annotation:

@JsonFormat(shape=JsonFormat.Shape.ARRAY)
class Endpoint {
}

and it should serialize entries as you wish.

Also: it'd be safest to then use @JsonPropertyOrder({ .... } ) to enforce specific ordering, as JVM may or may not expose fields or methods in any specific order.

Unable to deserialize Object Array via Jackson Yaml

The solution was quite simple:

actions: !<java.util.ArrayList>

  • arguments:
    • 90.00
      action: !<com.walterjwhite.stackoverflow.Action> Left

Deserialize a json array to objects using Jackson and WebClient

For the response to be matched with AccountOrderList class, json has to be like this

{
"accountOrders": [
{
"symbol": "XRPETH",
"orderId": 12122,
"clientOrderId": "xxx",
"price": "0.00000000",
"origQty": "25.00000000",
"executedQty": "25.00000000",
"status": "FILLED",
"timeInForce": "GTC",
"type": "MARKET",
"side": "BUY",
"stopPrice": "0.00000000",
"icebergQty": "0.00000000",
"time": 1514558190255,
"isWorking": true
},
{
"symbol": "XRPETH",
"orderId": 1212,
"clientOrderId": "xxx",
"price": "0.00280000",
"origQty": "24.00000000",
"executedQty": "24.00000000",
"status": "FILLED",
"timeInForce": "GTC",
"type": "LIMIT",
"side": "SELL",
"stopPrice": "0.00000000",
"icebergQty": "0.00000000",
"time": 1514640491287,
"isWorking": true
},
....
]
}

This is what the error message says "out of START_ARRAY token"

If you cannot change the response, then change your code to accept Array like this

this.webClient.get().uri(payload).accept(MediaType.APPLICATION_JSON)
.retrieve().bodyToMono(AccountOrder[].class).log();

You can convert this array to List and then return.



Related Topics



Leave a reply



Submit