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

{
"sys":
{
"country":"GB",
"sunrise":1381107633,
"sunset":1381149604
},
"weather":[
{
"id":711,
"main":"Smoke",
"description":"smoke",
"icon":"50n"
}
],
"main":
{
"temp":304.15,
"pressure":1009,
}
}

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.

最新文章

  1. java基础2_运算符,选择语句
  2. 抓包工具charles的使用
  3. [原]C# Winform 文件编码批量转换工具
  4. WebsitePanel 2.1.0beta配置部分
  5. Codeforces Gym 100463A Crossings 逆序数
  6. LeetCode25 Reverse Nodes in k-Group
  7. HDOJ/HDU 1180 诡异的楼梯(经典BFS-详解)
  8. Google表格
  9. BZOJ 1563 诗人小G
  10. poj1623 Squadtrees
  11. Linux 利用hosts.deny 防止暴力破解ssh(转)
  12. Python爬虫入门教程 4-100 美空网未登录图片爬取
  13. 学习Flask框架
  14. 【C++】C++中变量的声明与定义的区别
  15. 与vnpy相关的有用博客网址
  16. laravel中消息通知功能
  17. 面向对象【day07】:类的特性介绍(四)
  18. python 线程间通信之Condition, Queue
  19. 编写SHELL脚本--编写简单脚本
  20. 一篇谈Flink不错的文章

热门文章

  1. Linux 学习 (八) Shell
  2. [SimplePlayer] 7. 多线程处理
  3. vue 拖拽移动(类似于iPhone虚拟home )
  4. ORACLE表数据误删除的恢复方法(提交事务也可以)
  5. Pthread 用法笔记
  6. (五)qt资源文件
  7. linux下串口函数
  8. elk 中kafka启动脚本和配置文件
  9. [物理学与PDEs]第5章第3节 守恒定律, 应力张量
  10. APPLE-SA-2019-3-25-6 iCloud for Windows 7.11