A lightweight package that will read JSON data, expand and collapse JSON view accordingly.
preview.mp4
Easiest way to format Json String
<kr.pokeum.jsonviewer_xml.JsonRecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:text="{PUT_YOUR_JSON_STRING}" />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-xml:0.0.1'-
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 }
Add
JsonRecyclerViewin XML layout file:<kr.pokeum.jsonviewer_xml.JsonRecyclerView android:id="@+id/jsonViewer" android:layout_width="match_parent" android:layout_height="wrap_content" app:text="{PUT_YOUR_JSON_STRING}" />
Change
JsonRecyclerViewtext from the code:findViewById<JsonRecyclerView>(R.id.jsonViewer).setText("{PUT_YOUR_JSON_STRING}")
Add
RecyclerViewin XML layout file:<androidx.recyclerview.widget.RecyclerView android:id="@+id/jsonViewer" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" tools:ignore="SpeakableTextPresentCheck" tools:listitem="@layout/item_json_object" />
Set Adapter in
RecyclerView:val recyclerView = findViewById<RecyclerView>(R.id.jsonViewer) recyclerView.adapter = JsonViewerAdapter(/* JsonElement */)
-
Example - Alphabetically
JsonParser.Builder() .setComparator(compareBy { it.key }) .build()
Save and Restore Data on Configuration Changed in Android using Bundle
class YourActivity : AppCompatActivity() { private var jsonElement: JsonElement? = null override fun onCreate(savedInstanceState: Bundle?) { // ... if (savedInstanceState != null) { jsonElement = savedInstanceState.getParcelable("JSON_ELEMENT_KEY") /* Restore */ } } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) outState.putParcelable("JSON_ELEMENT_KEY", jsonElement) /* Save */ } }
![]() |
|
|---|---|
| Key | "friends", "0", "name", "age" |
| Value | "Alice", 28 |
| Splitter | : |
| Type | ABC, 123 |
| Arrow | \/ |
| Bracket | [ ], { } |
| Divider | │ |
<kr.pokeum.jsonviewer_xml.JsonRecyclerView
...
app:keyColor="@color/key_color"
app:valueColor="@color/value_color"
app:splitterColor="@color/splitter_color"
app:typeColor="@color/type_color"
app:arrowColor="@color/arrow_color"
app:bracketColor="@color/bracket_color"
app:dividerColor="@color/divider_color" />recyclerView.adapter = JsonViewerAdapter(/* JsonElement */).apply {
setKeyColor(JVColor(/* Default Color[, Dark Mode Color] */))
setValueColor(JVColor(/* ... */))
setSplitterColor(JVColor(/* ... */))
setTypeColor(JVColor(/* ... */))
setArrowColor(JVColor(/* ... */))
setBracketColor(JVColor(/* ... */))
setDividerColor(JVColor(/* ... */))
}
