2017-07-04 3 views
0

J'ai une exigence pour convertir un ensemble de structure de fichier obtenu à partir de DB dans un JSON. Par exemple: De DB, je reçois le chemin suivant et attributs de fichier:Comment faire pour convertir la liste des chemins de fichier en un objet JSON en Java

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" 

et ainsi de suite .. je dois convertir en un JSON comme:

[{ 
    "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

ce que vous avez essayé ...? coller du code ici –

+0

J'ai essayé le code ici: https://stackoverflow.com/questions/10660167/creating-a-tree-using-json-from-a-list-table/10660468#10660468 Mais cela rend l'objet des enfants , alors que j'ai besoin d'une liste – pg26

+0

@Sinistro aucune idée sur la façon dont t o convertir cela en une structure arborescente? – pg26

Répondre

1

Je suis va vous donner une solution jackson. D'abord, construisez un objet (ou plus, Je vous laisse gérer l'héritage Java, ou utiliser n'importe quel type de structure que vous voulez utiliser). Comme celui-ci par exemple:

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

Ensuite, créez le CustomSerializer, qui vous aidera à faire tout ce que vous voulez faire:

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

} 

Enfin (je l'ai pas fait, mais vous Je vais le faire, j'en suis sûr!) Créez un mappeur à partir de ce que vous avez lu jusqu'à la classe "Quelque chose". Je construire l'objet manuellement ici (rapidement, il est donc pas propre):

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

Et voici ce que je reçois:

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

Cordialement,

+0

Merci @Grai. Mais dans mon cas, je peux avoir la profondeur n, et dans ce cas, la structure de l'objet ne fonctionnera pas. Besoin d'une structure d'arbre type de solution. Et puis, je peux utiliser le Gson pour le convertir. – pg26

+0

Salut, Oui, la solution d'arbre pourrait être plus propre. Le plus important est que vous pouvez utiliser ce sérialiseur personnalisé pour sérialiser votre objet de données. – Grai