A lightweight package that will read JSON data, expand and collapse JSON view accordingly.
preview.mp4
Easiest way to format Json String
JsonViewerAdapter(jsonElement = {PUT_YOUR_JSON_ELEMENT})Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
// ...
maven { url 'https://jitpack.io' }
}
}Add the dependency
implementation 'com.github.pokeum:jsonviewer-compose:0.0.0'-
Convert
Stringinto aJsonElementobjectExample - Parsing JSON
val jsonString = "{ \"abc\": \"def\",\"xyz\": 123 }" val jsonParser = JsonParser.Builder().build() val jsonElement: JsonElement? = try { jsonParser.parse(jsonString) } // Raise a JSONException if it is not a JSONObject or JSONArray. catch (e: JSONException) { null }
JsonViewerAdapter(jsonElement = {PUT_YOUR_JSON_ELEMENT})
-
Example - Alphabetically
JsonParser.Builder() .setComparator(compareBy { it.key }) .build()
![]() |
|
|---|---|
| Key | "friends", "0", "name", "age" |
| Value | "Alice", 28 |
| Splitter | : |
| Type | ABC, 123 |
| Arrow | ∨ |
| Bracket | [ ], { } |
| Divider | │ |
JsonViewerAdapter(
// ...
keyColor = JsonViewerColor(colorResource(R.color.key_color)),
valueColor = JsonViewerColor(colorResource(R.color.value_color)),
splitterColor = JsonViewerColor(colorResource(R.color.splitter_color)),
typeColor = JsonViewerColor(colorResource(R.color.type_color)),
arrowColor = JsonViewerColor(colorResource(R.color.arrow_color)),
bracketColor = JsonViewerColor(colorResource(R.color.bracket_color)),
dividerColor = JsonViewerColor(colorResource(R.color.divider_color))
)
