JSON data usage in Android

Source: Internet
Author: User
Tags tojson

JSON (JavaScript Object Notation): A lightweight data Interchange format based on a subset of JavaScript. JSON makes JSON an ideal data exchange language, using a completely language-independent text format. Easy to read and write, but also easy to machine parse and build (network transfer speed). As a data interchange format, the JSON advantage is obvious (relative to the XML we typically use): JSON issmaller than XML; JSON and JavaScript interaction are more convenient; JSON is much faster than XML; the parsing of JSON is more convenient than parsing XML.

JSON syntax rules:

  • Data in name/value (key value) pairs
  • Data separated by commas (,)
  • Curly braces {} Save Object
  • Square (middle) brackets [] Save array the writing format for JSON data is: name/value pairs. The name is mainly denoted by the string "", and the value can be in multiple formats, including
  • Number (integer or floating point)
  • String (in double quotation marks (""))
  • Logical value (TRUE or FALSE)
  • Array (in square brackets)
  • Object (in curly braces)
  • Null
The following is an example of a number in JSON format:

{
"Tel": ["12345678", "87654321"],//array form, save in square brackets
"Name": "Tianjiefeng",//String
"Age": 20,//value
"Address": {"Country": "China", "province": "Guangdong"},//JSON object, save in curly braces
"graduate": TRUE//Boolean
}

How is the data in JSON format created in that Android development? And how to parse it? Let's start with an example of how to create the following JSON:

Private Jsonobject Createjsondata () {
The outermost is the curly brace {}, so it's a Jsonobject object
Jsonobject student = new Jsonobject ();
try {
The 1th value is [], which is a Jsonarray object
Jsonarray tel= new Jsonarray ();
Phone.put ("12345678");
Phone.put ("87654321");
Person.put ("Tel", tel);
2nd value A String
Person.put ("name", "Tianjiefeng");
3rd value A String value
Person.put ("Age", 20);
4th Value A Jsonobject object
Jsonobject address = new Jsonobject ();
Address.put ("Country", "China");
Address.put ("Province", "Guangdong");
Person.put ("Address", address);
5th value A Boolean value of
Person.put ("Graduate", true);
} catch (Jsonexception e) {
E.printstacktrace ();
}
ReturnStudent;
}

If you want to return string string, you only need to modify it to return student.tostring (); Let's see how to parse the JSON data above:

Private map<string, object> Parsejsondata (String json) {
map<string, object> resultmap = new hashmap<string, object> ();
try {
Converts a JSON string directly into a Jsonobject object
Jsonobject student= New Jsonobject (JSON);
1th Key-value pairs
Resultmap.put ("Tel", Person.getjsonarray ("tel"). toString ());
2nd Key-value pairs
Resultmap.put ("Name", Person.getstring ("name"));
3rd Key-value pairs
Resultmap.put ("Age", Person.getint ("Age"));
4th Key-value pairs
Resultmap.put ("Address", Person.getjsonobject ("address"). ToString ());
5th Key-value pairs
Resultmap.put ("Graduate", Person.getboolean (" Married "));
} catch (Jsonexception e) {
E.printstacktrace ();
}
return resultmap;
}
}That's what we use .JsonobjectandJsonarray for data manipulation. But in actual development, we typically use Gson to parse JSON data
First, download the GSONAPI and related jar packages from code.google.com/p/google-gson/downloads/list, and then process the data through the Gjson encapsulated method. the parsing of Gson is very simple, but its parsing rule is that it must have a bean file whose contents correspond to the JSON data type one by one. Gson has two important methods, one is Tojson (Tojson is to convert the contents of the bean into JSON content), one is Fromjson (encapsulates a single Bean object from our JSON object). Its simple example can be consulted: http://blog.csdn.net/kongzhichen/article/details/10135051



JSON data usage in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.