How to read Json
data class Entry(val email: String, val name: String)
@Composable
fun EntryText(entries: Map<String, Entry>) {
LazyColumn {
items(entries.entries.toList()) { (_, entry) ->
Column {
Text("Email: ${entry.email}")
Text("Name: ${entry.name}")
Spacer(modifier = Modifier.height(16.dp)) // Add spacing between entries
}
}
}
}
@Composable
fun DisplayJsonEntries() {
val gson = Gson()
// Replace "path/to/your/file.json" with the actual path to your JSON file
val jsonFile = File("/storage/emulated/0/Android/data/com.example.jsonparsing/files/form_data.json")
val jsonString = jsonFile.readText()
// Parse JSON string to a map where keys are entry IDs and values are Entry objects
val entries: Map<String, Entry> = gson.fromJson(jsonString, object : TypeToken<Map<String, Entry>>() {}.type)
// Display the content of each entry using EntryText composable
EntryText(entries = entries)
}
Comments
Post a Comment