如何在Java中将文件路径列表转换为JSON对象

时间:2017-07-04 06:56:09

标签: java json file tree gson

我需要将从DB获取的一组文件结构转换为JSON。 例如: 从DB,我得到以下路径和文件属性:

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"

等等.. 我需要将其转换为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"
                        }]
                    }
                ]
            }]
        }]
    }]
}]

1 个答案:

答案 0 :(得分:1)

我打算给你一个'杰克逊'解决方案。

首先,构建一个对象(或更多,我让你处理Java继承,或使用你想要使用的任何类型的结构)。 像这样一个例子:

 @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;}
}

然后,创建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();
    }

}

最后(我没有做过这个,但你会做到我确定!)从你读到的“Something”类创建一个mapper。 我在这里手动构建对象(很快,所以它不干净):

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);
}

这就是我得到的:

    {
   "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"
      }
   ]
}

此致