Get the App
SLTechnology News&Howtos  ›  Development  › 

How to parse and read data in Android

Shulou Source: shulou.com Published: 2022-06-02 10:08:59 09月19日 Update

This article shows you how to parse and read data in Android. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Data parsing and reading

The organization of data in network transmission: xml, json, http

(1) xml: extensible markup language

Features: save a small amount of data, cross-platform

Parsing methods: sax, dom, pull

A. sax parsing:

Features: high speed, less memory. Event-driven, not all content is loaded into memory at once.

StartElement: element node start

LocalName: tag name without prefix

QName: tag name with prefix

Attributes: attributes and values within a node

Xiao Ming

(2) dom resolution:

Features: load all content into memory at once

For server-side programming

(3) pull resolution:

Features: similar to sax, not automatically read

Suitable for mobile devices, taking up less memory

Json parsing

Features: more space-saving than xml

(1) json object: {"key1": "value1", "key2": "value2"}

a. Start and end with {}

B. Key must be a string, and value can be the underlying data type, String, json object, json array

c. Each piece of data is separated by a comma

Example:

{"name": "Xiaoming", "age": 25, "married": "true"}

(2) Analysis of json

/ / get an object of type JSONObject based on the string passed in

JSONObject jsonObject = new JSONObject (str)

/ / obtain the corresponding data according to key

String name = jsonObject.getString ("name")

Int age = jsonObject.getInt ("age")

Boolean b = jsonObject.getBoolean ("married")

/ / pass data into jsonOnject

JsonObject.put ("name", "Xiao Hong")

JsonObject.put ("age", 20)

JsonObject.put ("married", "false")

(3) JSONArray json array

a. Start and end with []

b. Each piece of data is a jsonObject

c. Each piece of data is separated by a comma

Example:

[{"name": "Xiao Ming", "age": 25, "sex": "male"}, {"name": "Xiao Hong", "age": 23, "sex": "female"}]

Parsing a piece of data in JSONArray format

/ / get an object of type JSONArray based on the passed string

JSONArray jsonArray = new JSONArray (str)

For (int item0; I

Tags: Data objects characteristics memory content characters strings types Xiaoming one-time examples prefixes skills arrays methods knowledge nodes commas Xiao Hong a. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn MySQL Shulou Information Shulou Technology