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 ...
随机推荐
- 18.flannel的配置
Kubernetes网络通信: (1) 容器间通信:同一个Pod内的多个容器间的通信, lo (2) Pod通信:Pod IP <--> Pod IP (3) Pod与Service通信: ...
- python之函数初识
一.函数的初识1.如何定义函数:def 函数名(): 代码... 例如:def my_len(): l = [1, 2, 3, 4] count = 0 for i in l: count += 1 ...
- JQ初级
一.认识jQuery 1.什么是jQuery jQuery是对原生JavaScript二次封装的工具函数集合 jQuery是一个简洁高效的且功能丰富的JavaScript工具库 2.jQuery的优势 ...
- #!/usr/bin/python3的作用 解决vscode ImportError: No module named xxxx
在 Python 脚本的第一行经常见到这样的注释: #!/usr/bin/env python3 或者 #!/usr/bin/python3 含义 在脚本中, 第一行以 #! 开头的代码, 在计算机行 ...
- 洛谷P3806 点分治
点分治 第一次写点分治..感觉是一个神奇而又暴力的东西orz 点分治大概就是用来处理树上链的信息,把路径分成过点x和不过点x的两种,不过点x的路径可以变成过点x的子树中一点的路径,递归处理 #incl ...
- 使用Redisson实现分布式锁,Spring AOP简化之
源码 Redisson概述 Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid).它不仅提供了一系列的分布式的Java常用对象,还提供了许多 ...
- Harbo1.5.2离线搭建
环境说明 操作系统版本:Centos7.5 docker版本:docker-ce 17.03.2 harbor版本:v1.5.2 docker-compose: 1.22.0 基础环境搭建 系统优化 ...
- python django(forms组件)
forms组件最大的作用,就是做数据校验. 普通做法,一个一个写校验规则,没有解耦.校验规则,都在视图函数里面. 网页校验 修改urls.py,增加路径addbook from app01 impor ...
- 第二节:框架前期准备篇之AutoFac常见用法总结
一. 说在前面的话 凡是大约工作在两年以上的朋友们,或多或少都会接触到一些框架搭建方面的知识,只要一谈到框架搭建这个问题或者最佳用法这个问题,势必会引起一点点小小的风波,我说我的好,他说他的好,非常容 ...
- (一)校园信息通微信小程序从前端到后台整和笔记
前段时间接触了微信小程序,现在回过头来做一些笔记. 先上效果图 后台数据管理界面(PHP) 校园信息通微信小程序前端界面 下面先简单的说一下怎样部署一个微信小程序 首先是前端 微信小程序有它专门的开发 ...