pokeum / jsonviewer-xml

Android JSON Viewer (XML)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Android JSON Viewer

Generic badge Generic badge


JSON Viewer and Beautifier

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}" />

Table of Contents

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'
  • JsonParser.parse()

    Convert String into a JsonElement object

    Example - 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 }

    Display JSON

    [Method 1] Use JsonRecyclerView

    Add JsonRecyclerView in 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 JsonRecyclerView text from the code:

    findViewById<JsonRecyclerView>(R.id.jsonViewer).setText("{PUT_YOUR_JSON_STRING}")

    [Method 2] Use RecyclerView

    Add RecyclerView in 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 */)
  • JsonParser - Sort JSON

    Example - Alphabetically

    JsonParser.Builder()
      .setComparator(compareBy { it.key })
      .build()

    JsonElement - Save and Restore

    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 */
      }
    }

Color

Key "friends", "0", "name", "age"
Value "Alice", 28
Splitter :
Type ABC, 123
Arrow \/
Bracket [ ], { }
Divider

Use JsonRecyclerView

<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" />

Use RecyclerView

recyclerView.adapter = JsonViewerAdapter(/* JsonElement */).apply {
  setKeyColor(JVColor(/* Default Color[, Dark Mode Color] */))
  setValueColor(JVColor(/* ... */))
  setSplitterColor(JVColor(/* ... */))
  setTypeColor(JVColor(/* ... */))
  setArrowColor(JVColor(/* ... */))
  setBracketColor(JVColor(/* ... */))
  setDividerColor(JVColor(/* ... */))
}

About

Android JSON Viewer (XML)


Languages

Language:Kotlin 95.8%Language:Ruby 4.2%