Skip to content

Spoke Right

Education for everyone

https://spokeright.com
Primary Menu
  • Home
  • Our Services
    • Translation
    • Interpretation
    • Transcription
    • Voice-Over
    • Proofreading
    • Content writing
  • Free Education
    • Learn Android Studio
    • Learn Python
    • Learn MongoDB
    • Learn MySql
    • Learn React
    • Montessori Education
  • Spoke Right News
  • Earn Online
  • Classic Games by Spoke Right
    • TicTac
    • Checkers
    • Foosball
    • Billiards
    • Master Chess
    • Logic Game
  • Contact Us
Download Now
  • Home
  • Learn Android Studio
  • What is JSON parser Android
  • Learn Android Studio

What is JSON parser Android

Spoke Right November 18, 2022 2 min read

JSON Parser ANdroid

JSON stands for the Javascript Object Notation. It is a programming language, minimal, textual, and a subset of JavaScript and an alternative to XML. We can parse the JSON object and array in Android.

Advantages of JSON over XML:

  • Compared to XML, JSON is easier and faster for AJAX applications.
  • Compared to XML, JSON is quicker and shorter to read and write.
  • JSON uses an array.
  • JSON is Human Readable.
  • JSON is easy to parse.

Advantages of XML over JSON:

  • XML supports Namespaces to allow for the sharing of standard structures.
  • Compared to JSON, XML supports better representation for inheritance.
  • XML is Human Readable.
  • XML is easy to parse.

JSON object:

Similar to a map, the JSON object also contains key/value pairs, separated by a comma, where keys are strings and the values are the JSON types. The JSON object is represented by the { (curly brace).

Example:

{  
    "student": {  
        "name": "Rohan",   
        "id": 100,   
        "class": 6  
    }  
}

JSON array:

The JSON array is represented by the [ (square bracket).

Example 1:

["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

Example 2:

{ "Student" :  
    [  
     {"id":"1","name":"Rohan","marks":"80"},  
     {"id":"2","name":"Rohit","marks":"90"}  
    ]   
}

Parsing JSONArray in Android:

In the below example, we will parse the JSONArray containing the JSON Objects using the JSONArray class.

Example:

activity_main.xml

In the activity_main.xml file, we will drag a Textview from the palette.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/user_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dp" />
</LinearLayout>

List_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="17dp" />
    <TextView
        android:id="@+id/designation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_marginTop="7dp"
        android:textColor="#343434"
        android:textSize="14dp" />
    <TextView
        android:id="@+id/location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/designation"
        android:layout_alignBottom="@+id/designation"
        android:layout_alignParentRight="true"
        android:textColor="#343434"
        android:textSize="14dp" />
</RelativeLayout>

Activity class:

In the MainActivity.java file, we will write the code to parse the XML using the JSON parser.

package com.example.radioapp;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String jsonStr = getListData();
        try{
            ArrayList<HashMap<String, String>> userList = new ArrayList<>();
            ListView lv = (ListView) findViewById(R.id.user_list);
            JSONObject jObj = new JSONObject(jsonStr);
            JSONArray jsonArry = jObj.getJSONArray("users");
            for(int i=0;i<jsonArry.length();i++){
                HashMap<String,String> user = new HashMap<>();
                JSONObject obj = jsonArry.getJSONObject(i);
                user.put("name",obj.getString("name"));
                user.put("designation",obj.getString("designation"));
                user.put("location",obj.getString("location"));
                userList.add(user);
            }
            ListAdapter adapter = new SimpleAdapter(MainActivity.this, userList,
 R.layout.list_row,new String[]{"name","designation","location"},
 new int[]{R.id.name, R.id.designation, R.id.location});
            lv.setAdapter(adapter);
        }
        catch (JSONException ex){
            Log.e("JsonParser Example","unexpected JSON exception", ex);
        }
    }
    private String getListData() {
        String jsonStr = "{ \"users\" :[" +
                "{\"name\":\"A\",\"designation\":\"Leader\",\"location\":\"City1\"}" +
                ",{\"name\":\"B\",\"designation\":\"Secretary\",\"location\":\"City2\"}" +
                ",{\"name\":\"C\",\"designation\":\"Accountant\",\"location\":\"City3\"}] }";
        return jsonStr;
    }
}

Continue Reading

Previous: What is Android XML Parsing using DOM Parser
Next: What is media player for Android

Related Stories

How to Get Imageview From Sqlite Database Android Studio
1 min read
  • Learn Android Studio

How to Get Imageview From Sqlite Database Android Studio

November 28, 2022
How do you show a toast on Android
1 min read
  • Learn Android Studio

How do you show a toast on Android

November 28, 2022
How do I completely Android Java Close App
1 min read
  • Learn Android Studio

How do I completely Android Java Close App

November 27, 2022

This AD Will support Us

Support Us

Coding Ustad LTD Support
Coding Ustad LTD Support

Coding Ustad LTD

Coding Ustad LTD
Coding Ustad LTD

Recent Posts

  • Imran Khan, Former Prime Minister, Takes TikTok by Storm with Record-Breaking Followers and Likes
  • How to Install Node.js and npm on CentOS 7
  • How to Install Node.js and npm on CentOS 7
  • How to Install CentOS Web Panel (CWP) on CentOS 7
  • The Power of Marketing in Real Estate: A Guide to Boost Your Business

Get free Hosting

Hostens.com - A home for your website
Ahsan Nadeem

You may have missed

Imran Khan, Former Prime Minister, Takes TikTok by Storm with Record-Breaking Followers and Likes
2 min read
  • Spoke Right News

Imran Khan, Former Prime Minister, Takes TikTok by Storm with Record-Breaking Followers and Likes

July 18, 2023
How to Install Node.js and npm on CentOS 7
4 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install Node.js and npm on CentOS 7

May 3, 2023
How to Install Node.js and npm on CentOS 7
4 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install Node.js and npm on CentOS 7

May 2, 2023
How to Install CentOS Web Panel (CWP) on CentOS 7
3 min read
  • Coding Ustad LTD
  • Education for Everyone

How to Install CentOS Web Panel (CWP) on CentOS 7

May 2, 2023
  • Home
  • Our Services
  • Free Education
  • Spoke Right News
  • Earn Online
  • Classic Games by Spoke Right
  • Contact Us
Copyright © All rights reserved. | MoreNews by AF themes.