Android - JSON Parser Tutorial
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. |
Android - JSON Parser Tutorial的更多相关文章
- android JSON 技术
json 语法检查和在线解析网址:http://json.parser.online.fr/ limengwe android Json解析详解(详细代码) http://blog.csdn.net/ ...
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例 继上篇json解析,我用了原生的json解析,但是在有些情况下我们不得不承认,一些优秀的json解析框架确实十分的 ...
- android+json+php+mysql实现用户反馈功能
相信每个项目都会有用户反馈建议等功能,这个实现的方法很多,下面是我实现的方法,供大家交流.首先看具体界面,三个字段.名字,邮箱为选填,可以为空,建议不能为空.如有需要可以给我留言. 下面贴出布局代码, ...
- Android JSON原生解析的几种思路,以号码归属地,笑话大全,天气预报为例演示
Android JSON原生解析的几种思路,以号码归属地,笑话大全,天气预报为例演示 今天项目中要实现一个天气的预览,加载的信息很多,字段也很多,所以理清了一下思路,准备独立出来写一个总结,这样对大家 ...
- android json 解析简单实例
Android JSON解析跟JAVA 的JSON解析原理是一样的. Android自带的JSON方式跟方便,不需要导包啥的.不深究原理了,直接上代码: public class JsonActivi ...
- android json解析及简单例子+Android与服务器端数据交互+Android精彩案例【申明:来源于网络】
android json解析及简单例子+Android与服务器端数据交互+Android精彩案例[申明:来源于网络] android json解析及简单例子:http://www.open-open. ...
- Android JSON 解析关键代码
Android Json 解析其实还是蛮重要的知识点,为什么这么说呢,因为安卓通信大部分的协议都是使用 json 的方式传输,我知道以前大部分是使用的 xml ,但是时代在发展社会在进步,json 成 ...
- VBA json parser[z]
http://www.ediy.co.nz/vbjson-json-parser-library-in-vb6-xidc55680.html VB-JSON: A Visual Basic 6 (VB ...
- Android Json解析与总结
一.JSON定义 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Progra ...
随机推荐
- python 基本数据类型以及内置方法
一.数字类型 # 一.整型int # ======================================基本使用====================================== ...
- python常用函数用法整理
1,zeros函数(同理的还有ones函数) http://www.jb51.net/article/127649.htm 注意: (m,n)是生成m行n列的矩阵,但要生成二维矩阵的时候要用两层括号, ...
- 【Android O】 Service AAA does not have a SELinux domain defined
在init.AAA.rc里面添加了一个脚本启动的服务: service AAA /vendor/bin/sh /vendor/etc/AAA_spec.sh user root group root ...
- 【bfs】抓住那头牛
[题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...
- Python import hook
转自http://blog.konghy.cn/2016/10/25/python-import-hook/,这里有好多好文章 import hook 通常被译为 探针.我们可以认为每当导入模块的时候 ...
- [TJOI2007] 调整队形
题目链接 区间 DP 的经典模型之一. 题意是将整个串通过四种操作变成一个回文串,根据套路,不难设计出 dp[i][j] 表示为使区间 [i, j] 成为回文串的最少操作次数. 先判断 a[i] 是否 ...
- vmware(1):vmware中的bridge、nat、host-only的区别
VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式) bridged(桥接模式) 在这种模式下,VMWare虚拟出来的操作系统就 ...
- JGUI源码:Accordion兼容IE8实现(3)
本来不考虑IE8,但是还是有部分客户用的XP,有不代表没有,尽量做一下兼容处理1.before,after,要使用:不能使用:: 2.阻止冒泡 function stopPropagation(e) ...
- SQL Server 跨服务器操作
Ø 简介 在工作中编写 SQL 时经常会遇到跨库或跨服务器操作,比如查询时,通过 A 服务器的某张表关联 B 服务器某张表,进行连接查询.或者从另一台服务器中的数据,对当前数据库中的数据进行 CRU ...
- 拖动DIV
链接:https://www.cnblogs.com/joyco773/p/6519668.html 移动端:div在手机页面上随意拖动 1 <!doctype html> 2 & ...