Ad esempio: dato questo documento JSON:lista di uscita di tutti i percorsi di nodi foglia in un documento JSON in java
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
Vorrei produrre (qualcosa di simile) questa uscita:
store.book.category: "reference"
store.book.author: "Nigel Rees"
store.book.title: "Sayings of the Century"
store.book.price: 8.95
store.book.category: "fiction"
store.book.author: "Herman Melville"
store.book.title: "Moby Dick"
store.book.isbn: "0-553-21311-3"
store.book.price: 8.99
store.bicycle.color: "red"
store.bicycle.price: 19.95
expensive:10
Piuttosto che lavorare con il testo non elaborato, preferirei una soluzione efficiente basata su una delle robuste librerie json (gson, jackson, ecc.).
una possibile soluzione potrebbe utilizzare JsonNode dalla libreria Jackson Jackson. JsonNode rootNode = mapper.readTree (fileReader); – Hector
Se si utilizza un parser di streaming (come Jackson), è possibile mantenere una pila delle proprietà in cui ci si trova attualmente. quando colpisci un oggetto terminale (stringa, numero, booleano), emetti lo stack + il letterale. Potresti non aver nemmeno bisogno dello stack se usi getParsingContext. –