Android – JSON Parser Tutorial

Android provides four different classes to manipulate
JSON data. These classes are
JSONArray,JSONObject,JSONStringer and
JSONTokenizer.

  1. {  
  2.    "sys":  
  3.    {  
  4.       "country":"GB",  
  5.       "sunrise":1381107633,  
  6.       "sunset":1381149604  
  7.    },  
  8.    "weather":[  
  9.    {  
  10.       "id":711,  
  11.       "main":"Smoke",  
  12.       "description":"smoke",  
  13.       "icon":"50n"  
  14.    }  
  15. ],  
  16. "main":  
  17.    {  
  18.       "temp":304.15,  
  19.       "pressure":1009,  
  20.    }  
  21. }  

JSON - Elements

An JSON file consist of many components. Here is the table defining the components of an JSON file and their description −

Sr.No Component & description
1 Array([)

In a JSON file , square bracket ([) represents a JSON array

2 Objects({)

In a JSON file, curly bracket ({) represents a JSON object

3 Key

A JSON object contains a key that is just a string. Pairs of key/value make up a JSON object

4 Value

Each key has a value that could be string , integer or double e.t.c

JSON - Parsing

For parsing a JSON object, we will create an object of class JSONObject and specify a string containing JSON data to it. Its syntax is:

String in;
JSONObject reader = new JSONObject(in);

The last step is to parse the JSON. An JSON file
consist of different object with different key/value pair e.t.c. So JSONObject
has a separate function for parsing each of the component of JSON file. Its
syntax is given below:


JSONObject sys  = reader.getJSONObject("sys");
country
= sys.getString("country");

JSONObject main = reader.getJSONObject("main");
temperature
= main.getString("temp"
);


The
method getJSONObject returns the JSON object. The
method getStringreturns the string value of the specified
key.


Apart from the these methods , there are other
methods provided by this class for better parsing JSON files. These methods are
listed below −



























Sr.No Method
& description
1 get(String
name)

This method just Returns the value but in the form of
Object type

2 getBoolean(String
name)

This method returns the boolean value specified by
the key

3 getDouble(String
name)

This method returns the double value specified by the
key

4 getInt(String
name)

This method returns the integer value specified by
the key

5 getLong(String
name)

This method returns the long value specified by the
key

6 length()

This method returns the number of name/value mappings
in this object..

7 names()

This method returns an array containing the string
names in this object.


 print?

  1.    "sys"
  2.    { 
  3.       "country":"GB"
  4.       "sunrise":1381107633
  5.       "sunset":1381149604 
  6.    }, 
  7.    "weather":[ 
  8.    { 
  9.       "id":711
  10.       "main":"Smoke"
  11.       "description":"smoke"
  12.       "icon":"50n" 
  13.    } 
  14. ], 
  15. "main"
  16.    { 
  17.       "temp":304.15
  18.       "pressure":1009
  19.    } 

JSON - Elements

An JSON file consist of many components. Here is the
table defining the components of an JSON file and their description −

Sr.No Component
& description
1 Array([)

In a JSON file , square bracket ([) represents a JSON
array

2 Objects({)

In a JSON file, curly bracket ({) represents a JSON
object

3 Key

A JSON object contains a key that is just a string.
Pairs of key/value make up a JSON object

4 Value

Each key has a value that could be string , integer
or double e.t.c

JSON - Parsing

For parsing a JSON object, we will create an object
of class JSONObject and specify a string containing JSON data to it. Its syntax
is:

String in;
JSONObject reader = new JSONObject(in);

The last step is to parse the JSON. An JSON file
consist of different object with different key/value pair e.t.c. So JSONObject
has a separate function for parsing each of the component of JSON file. Its
syntax is given below:

JSONObject sys  = reader.getJSONObject("sys");
country = sys.getString("country");
			
JSONObject main  = reader.getJSONObject("main");
temperature = main.getString("temp");

The
method getJSONObject returns the JSON object. The
method getStringreturns the string value of the specified
key.

Apart from the these methods , there are other
methods provided by this class for better parsing JSON files. These methods are
listed below −

Sr.No Method
& description
1 get(String
name)

This method just Returns the value but in the form of
Object type

2 getBoolean(String
name)

This method returns the boolean value specified by
the key

3 getDouble(String
name)

This method returns the double value specified by the
key

4 getInt(String
name)

This method returns the integer value specified by
the key

5 getLong(String
name)

This method returns the long value specified by the
key

6 length()

This method returns the number of name/value mappings
in this object..

7 names()

This method returns an array containing the string
names in this object.


暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇