Easy Tutorial
❮ Funny Programmer Pictures Decimal Decimals Are Converted To Binary Fractions ❯

7.2.2 Android JSON Data Parsing

Category Android Basic Beginner Tutorial

Introduction to This Section:

>

I believe everyone is familiar with JSON. The data transmission method we usually use to interact with the server is mostly in the form of JSON strings. To save an object, we can also write it as a JSON string and store it! Parsing JSON, whether you use Gson, Fastjson, jackson, etc., this section will not use these third-party parsing libraries. Instead, we will use Android's built-in JSON parser to parse JSON! Alright, let's get started with this section!


1. JSON Concepts and Comparison with XML

1) What is JSON?

>

Answer: JavaScript Object Notation, a lightweight data interchange format, just like XML, widely adopted as a solution for client-server interactions! It has good readability and is easy to write quickly.

2) Comparison between JSON and XML:

>

PS: The above is from Baidu. Simply put, the advantages of JSON: smaller volume, saves traffic, just not as intuitive as XML, and slightly worse readability!

3) JSON Format Specification:

>

Just like a protocol, there must be a set of specifications, as both parties transmit data through JSON strings. The syntax rules are as follows: data is in name/value pairs; data is separated by commas; curly braces hold objects; square brackets hold arrays; The writing format of JSON data: name/value pairs, for example: "person": "coder-pig". Here is a simple JSON string example:

[
    { "id":"1","name":"基神","age":"18" },
    { "id":"2","name":"B神","age":"18"  },
    { "id":"3","name":"曹神","age":"18" }
]

>

We can not only parse JSON but also splice JSON ourselves. Of course, if you splice a JSON string and are not sure if it's correct, you can search Baidu for a validation tool, such as: http://www.tutorialpro.org/jsontool, paste the JSON string and validate it!


2. JSON Parsing Classes Provided by Android

>

These APIs exist under the org.json package, and the classes we use include:


3. Code Example: Parsing JSON String:

PS: Here we will not write a servlet or request a website, but directly write the JSON into a string for parsing, just simulate it!

1) Simple JSON String Parsing Example:

We are parsing the simple JSON above, first let's write a POJO class:

Person.java:

/**
 * Created by Jay on 2015/9/8 0008.
 */
public class Person {
    private String id;
    private String name;
    private String age;
    public void setId(String id){
        this.id = id;
    }
    public String getId(){
        return this.id;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public void setAge(String age){
        this.age = age;
    }
    public String getAge(){
        return this.age;
    }
    @Override
    public String toString() {
        return this.name + "~年方:" + this.age;
    }
}

Write a method to parse the above JSON string:

private void parseEasyJson(String json){
    persons = new ArrayList<Person>();
    try{
        JSONArray jsonArray = new JSONArray(json);
        for(int i = 0;i < jsonArray.length();i++){
            JSONObject jsonObject = (JSONObject) jsonArray.get(i);
            Person person = new Person();
            person.setId(i+"");
            person.setName(jsonObject.getString("name"));
            person.setAge(jsonObject.getString("age"));
            persons.add(person);
        }
    }catch (Exception e){e.printStackTrace();}
}

Running Effect Image: -2.2.6 AbsoluteLayout (Absolute Layout)

-2.3.1 TextView (Text View) Detailed Explanation

-2.3.2 EditText (Input Box) Detailed Explanation

-2.3.3 Button and ImageButton

-2.3.4 ImageView (Image View)

-2.3.5 RadioButton (Radio Button) & Checkbox (Checkbox)

-2.3.6 ToggleButton and Switch

-2.3.7 ProgressBar (Progress Bar)

-2.3.8 SeekBar (Drag Bar)

-2.3.9 RatingBar (Star Rating Bar)

-2.4.1 ScrollView (Scroll View)

-2.4.2 Date & Time Components (Part 1)

-2.4.3 Date & Time Components (Part 2)

-2.4.4 Adapter Basics

-2.4.5 Simple Usage of ListView

-2.4.6 BaseAdapter Optimization

-2.4.7 ListView Focus Issues

-2.4.8 Solving ListView Checkbox Misalignment

-2.4.9 ListView Data Update Issues

-2.5.0 Building a Reusable Custom BaseAdapter

-2.5.1 Implementing Multi-Layout ListView Items

-2.5.2 Basic Usage of GridView (Grid View)

-2.5.3 Basic Usage of Spinner (Dropdown List)

-2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text View)

-2.5.5 Basic Usage of ExpandableListView (Expandable List)

-2.5.6 Basic Usage of ViewFlipper (Flip View)

-2.5.7 Basic Usage of Toast

-2.5.8 Detailed Explanation of Notification (Status Bar Notification)

-2.5.9 Detailed Explanation of AlertDialog (Dialog Box)

-2.6.0 Basic Usage of Other Common Dialogs

-2.6.1 Basic Usage of PopupWindow (Floating Window)

-2.6.2 Menu (Menu)

-2.6.3 Simple Usage of ViewPager

-2.6.4 Simple Usage of DrawerLayout (Official Side Sliding Menu)

-3.1.1 Event Handling Mechanism Based on Listeners

-3.2 Event Handling Mechanism Based on Callbacks

-3.3 Analysis of Handler Message Passing Mechanism

-3.4 TouchListener vs OnTouchEvent + Multi-Touch

-3.5 Listening to EditText Content Changes

-3.6 Responding to System Setting Changes (Configuration Class)

-3.7 AsyncTask Asynchronous Tasks

-3.8 Gestures (Gestures)

-4.1.1 Introduction to Activity

-4.1.2 Getting Started with Activity

-4.1.3 Advanced Activity

-4.2.1 Introduction to Service

-4.2.2 Advanced Service

-4.2.3 Expert Service

-4.3.1 Basic BroadcastReceiver

-4.3.2 Detailed BroadcastReceiver

-4.4.1 Introduction to ContentProvider

-4.4.2 Further Exploration of ContentProvider – Document Provider

-4.5.1 Basic Usage of Intent

-4.5.2 Passing Complex Data with Intent

-5.1 Basic Overview of Fragment

-5.2.1 Fragment Example - Bottom Navigation Bar Implementation (Method 1)

-5.2.2 Fragment Example - Bottom Navigation Bar Implementation (Method 2)

-5.2.3 Fragment Example - Bottom Navigation Bar Implementation (Method 3)

-5.2.4 Fragment Example - Bottom Navigation Bar + ViewPager Page Sliding

-5.2.5 Fragment Example - Simple Implementation of News (Shopping) App List Fragment

-6.1 Data Storage and Access - File Storage and Reading

-6.2 Data Storage and Access - SharedPreferences for Saving User Preferences

-6.3.1 Data Storage and Access - Introduction to SQLite Database

-6.3.2 Data Storage and Access - Further Exploration of SQLite Database

-7.1.1 Android Network Programming and HTTP Protocol Learning

-7.1.2 Android HTTP Request and Response Headers Learning

-7.1.3 Android HTTP Request Method: HttpURLConnection

-7.1.4 Android HTTP Request Method: HttpClient

-7.2.1 Android XML Data Parsing

-7.3.1 Android File Upload

-7.3.2 Android File Download (1)

-7.3.3 Android File Download (2)

-7.4 Android Calling WebService

-7.5.1 Basic Usage of WebView (Web View)

-7.5.2 Basic Interaction Between WebView and JavaScript

-7.5.3 Considerations for WebView in Android 4.4 and Later

-7.5.4 WebView File Download

WeChat Subscription

❮ Funny Programmer Pictures Decimal Decimals Are Converted To Binary Fractions ❯