Create a valid JSON using Jackson that contains an array and a custom field
By : A.H
Date : March 29 2020, 07:55 AM
may help you . I think you can use Hashmap . Jackson will understand how to apply filter to the Object in array and will just skip any other object(String/array) it finds. Here is demo, that works for me: code :
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import org.json.JSONException;
import java.io.IOException;
import java.util.HashMap;
public class test12 {
public static void main(String[] args) throws IOException, JSONException {
Object[] allUsers = get_all_users();
String[] ignorableFieldNames = {"skip"};
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.addMixIn(Object.class, PropertyFilterMixIn.class);
FilterProvider filters = new SimpleFilterProvider()
.addFilter("filter properties by name",
SimpleBeanPropertyFilter.serializeAllExcept(
ignorableFieldNames));
mapper.setFilterProvider(filters);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("users", allUsers);
map.put("uri", "/users");
String result = mapper.writeValueAsString(map);
System.out.println(result);
}
@JsonFilter("filter properties by name")
public static class PropertyFilterMixIn {
}
private static Object[] get_all_users() {
User user1 = new User();
user1.foo = "abc1";
user1.bar = "def1";
user1.skip = "this field is skipped";
User user2 = new User();
user2.foo = "abc2";
user2.bar = "def2";
user2.skip = "this field is skipped";
return new Object[]{user1, user2};
}
public static class User {
public String foo;
public String bar;
public String skip;
}
}
{
"users" : [ {
"foo" : "abc1",
"bar" : "def1"
}, {
"foo" : "abc2",
"bar" : "def2"
} ],
"uri" : "/users"
}
|
Simple nameless json array convert to mapped JS or PHP array
By : LikeSE
Date : March 29 2020, 07:55 AM
Does that help You have a 2 dimensional array. To split it into columns, you can just iterate the array and push the values into new arrays code :
var data = [[1,2],[1,2]];
var firstValues=[];
var secondValues=[];
for(var i=0; i < data.length; i++){
firstValues.push(data[i][0]);
secondValues.push(data[i][1]);
}
|
Parsing a nameless JSON array in Java
By : rafa00706
Date : March 29 2020, 07:55 AM
|
how to create json array using jackson
By : j v
Date : March 29 2020, 07:55 AM
I wish this help you Can you build the array as an object before writing it, rather than bothering with all the individual pieces?
|
Create a CN1 JSON Array from POJO for Jackson
By : Jawrow92
Date : March 29 2020, 07:55 AM
it helps some times I'm doing pretty similar things with my app ! Eg : SQLite database with cn1-data-access library for cn1 application and server side with hibernate and jackson plugin. I've developped method to map/unmap object recursively in cn1-data-access plugin.
|