[英]How to convert list of filepaths to a JSON object in Java
I've a requirement to convert a set of file structure obtained from DB into a JSON. 我需要将从DB获得的一组文件结构转换为JSON。 For example: From DB, I get the following path and file attributes:
例如:从数据库,我得到以下路径和文件属性:
Record 1:
"path": "/sd/card/camera/pics/selfie.jpg"
"fileName": "selfie.jpg",
"mimeType": "image/jpeg"
Record 2:
"path": "/sd/card/camera/pics/personal/selfie1.jpg"
"fileName": "selfie1.jpg",
"mimeType": "image/jpeg"
and so on.. I need to convert this to a JSON like: 等等。我需要将其转换为JSON,例如:
[{
"sd": [{
"card": [{
"camera": [{
"pics": [{
"fileName": "selfie.jpg",
"path": "/sd/card/camera/pics/selfie.jpg",
"mimeType": "image/jpeg"
},
{
"personal": [{
"fileName": "selfie1.jpg",
"path": "/sd/card/camera/pics/personal/selfie1.jpg",
"mimeType": "image/jpeg"
}]
}
]
}]
}]
}]
}]
I'm going to give you a 'jackson' solution. 我将为您提供一个“杰克逊”解决方案。
First, build an object (or more, I let you deal with the Java inheritance, or use any kind of structure you want to use ). 首先,构建一个对象(或者更多, 我让您处理Java继承,或者使用要使用的任何类型的结构 )。 Like this one by example :
像这样一个例子:
@JsonSerialize(using = CustomSerializer.class)
public class Something {
private String currentFolder; // Name of the folder if this instance of something is a folder
private Something[] childs;
private Map<String,String> currentPicture; // Picture properties if this instance of something is a picture
public Something() {currentPicture = new HashMap<String,String>();}
public Something[] getChilds() {
return childs;
}
public void setContent(Something[] _childs) {this.childs = _childs;}
public String getCurrentFolder() {return currentFolder;}
public void setCurrentFolder(String _currentFolder) {this.currentFolder = _currentFolder;}
public Map<String,String> getCurrentPicture() {return currentPicture;}
public void setCurrentPicture(Map<String,String> currentPicture) {this.currentPicture = currentPicture;}
}
Then, create the CustomSerializer, that will help you do whatever you want to do : 然后,创建CustomSerializer,它将帮助您完成您想做的所有事情:
public class CustomSerializer extends JsonSerializer<Something>{
@Override
public void serialize(Something value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
// Adding the folder into the json, only if it exists
if(value.getCurrentFolder()!=null){
jgen.writeObjectField(value.getCurrentFolder(), value.getChilds());
}
// Adding properties of the picture, only if they exist
if(value.getCurrentPicture()!= null){
for(String k : value.getCurrentPicture().keySet()){
jgen.writeObjectField(k,value.getCurrentPicture().get(k));
}
}
jgen.writeEndObject();
}
}
Finally (I've not done this one, but you'll do it I'm sure !) create a mapper from what you read to the "Something" class. 最后(我还没有做过,但是我敢肯定,您会做的!)从您阅读的内容到“ Something”类创建一个映射器。 I build the object manually here (quickly, so it's not clean):
我在这里手动构建对象(很快,所以不干净):
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
Something s = new Something();
s.setCurrentFolder("toto");
Something s2 = new Something();
s2.setCurrentFolder("tata");
Something s2bis = new Something();
s2bis.setCurrentFolder("tataBis");
Something[] s2Group = {s2bis};
s2.setContent(s2Group);
Something s2bispic = new Something();
s2bispic.getCurrentPicture().put("fileName", "ThatPictureOfMysSelfILikeSoMuch.jpg");
s2bispic.getCurrentPicture().put("path", "toto/tata/tataBis/ThatPictureOfMysSelfILikeSoMuch.jpg");
s2bispic.getCurrentPicture().put("mimeType", "image/jpeg");
Something s2bispic2 = new Something();
s2bispic2.getCurrentPicture().put("fileName", "ThatPictureOfMysSelfIDontLike.jpg");
s2bispic2.getCurrentPicture().put("path", "toto/tata/tataBis/ThatPictureOfMysSelfIDontLike.jpg");
s2bispic2.getCurrentPicture().put("mimeType", "image/jpeg");
Something[] s2BisGroup = {s2bispic,s2bispic2};
s2bis.setContent(s2BisGroup);
Something s3 = new Something();
s3.getCurrentPicture().put("fileName", "selfie.jpg");
s3.getCurrentPicture().put("path", "toto/selfie.jpg");
s3.getCurrentPicture().put("mimeType", "image/jpeg");
Something[] sGroup = {s2,s3};
s.setContent(sGroup);
ObjectMapper mapper = new ObjectMapper();
String temp = mapper.writeValueAsString(s);
System.out.println(temp);
}
And this is what I get : 这就是我得到的:
{
"toto":[
{
"tata":[
{
"tataBis":[
{
"path":"toto/tata/tataBis/ThatPictureOfMysSelfILikeSoMuch.jpg",
"fileName":"ThatPictureOfMysSelfILikeSoMuch.jpg",
"mimeType":"image/jpeg"
},
{
"path":"toto/tata/tataBis/ThatPictureOfMysSelfIDontLike.jpg",
"fileName":"ThatPictureOfMysSelfIDontLike.jpg",
"mimeType":"image/jpeg"
}
]
}
]
},
{
"path":"toto/selfie.jpg",
"fileName":"selfie.jpg",
"mimeType":"image/jpeg"
}
]
}
Regards, 问候,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.