原生JSON解析
原生JSON解析
JSONObject:JSON数据封装对象
JSONArray:JSON数据封装数组
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.bwie.jsonobject.MainActivity"> <Button
android:id="@+id/read_file_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取文件中的json数据"/> <Button
android:id="@+id/parse_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解析json数据"/> <TextView
android:id="@+id/result_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/> </LinearLayout>
Bean:
package net.bwie.jsonobject;
import java.util.List;
public class ShoppingBean {
private String msg;
private InfoBean info;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public InfoBean getInfo() {
return info;
}
public void setInfo(InfoBean info) {
this.info = info;
}
@Override
public String toString() {
return "ShoppingBean{" +
"msg='" + msg + '\'' +
", info=" + info +
'}';
}
public static class InfoBean {
private int cat_id;
private List<GoodsBean> good;
private boolean url;
public int getCat_id() {
return cat_id;
}
public void setCat_id(int cat_id) {
this.cat_id = cat_id;
}
public List<GoodsBean> getGood() {
return good;
}
public void setGood(List<GoodsBean> good) {
this.good = good;
}
public boolean isUrl() {
return url;
}
public void setUrl(boolean url) {
this.url = url;
}
@Override
public String toString() {
return "InfoBean{" +
"cat_id=" + cat_id +
", good=" + good +
", url=" + url +
'}';
}
public static class GoodsBean {
private long add_time;
private String colorcode;
private String currency_price;
private String description;
private String goods_id;
private String goods_name;
private String thumb;
public long getAdd_time() {
return add_time;
}
public void setAdd_time(long add_time) {
this.add_time = add_time;
}
public String getColorcode() {
return colorcode;
}
public void setColorcode(String colorcode) {
this.colorcode = colorcode;
}
public String getCurrency_price() {
return currency_price;
}
public void setCurrency_price(String currency_price) {
this.currency_price = currency_price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getGoods_id() {
return goods_id;
}
public void setGoods_id(String goods_id) {
this.goods_id = goods_id;
}
public String getGoods_name() {
return goods_name;
}
public void setGoods_name(String goods_name) {
this.goods_name = goods_name;
}
public String getThumb() {
return thumb;
}
public void setThumb(String thumb) {
this.thumb = thumb;
}
@Override
public String toString() {
return "GoodsBean{" +
"add_time=" + add_time +
", colorcode='" + colorcode + '\'' +
", currency_price='" + currency_price + '\'' +
", description='" + description + '\'' +
", goods_id='" + goods_id + '\'' +
", goods_name='" + goods_name + '\'' +
", thumb='" + thumb + '\'' +
'}';
}
}
}
}
Activity:
/**
* 1、将json文件存在外部存储中,使用IO流读取文件中的数据
* 2、使用JSONObject解析读取出来的数据
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private String mJson = ""; protected Button mReadFileBtn;
protected Button mParseBtn;
protected TextView mResultTv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
initView();
} @Override
public void onClick(View view) {
if (view.getId() == R.id.read_file_btn) {
readFile();
} else if (view.getId() == R.id.parse_btn) {
ShoppingBean shopping = parseJson();
Log.d("1507", "" + shopping);
}
} // 解析JSON数据
// 遇到{}就创建JSONObject,遇到[]就创建JSONArray
private ShoppingBean parseJson() {
ShoppingBean shopping = null;
try {
JSONObject rootObject = new JSONObject(mJson);
shopping = new ShoppingBean(); String msg = rootObject.getString("msg");
shopping.setMsg(msg); JSONObject infoObject = rootObject.getJSONObject("info");
ShoppingBean.InfoBean info = new ShoppingBean.InfoBean();
shopping.setInfo(info); int catId = infoObject.getInt("cat_id");
info.setCat_id(catId); boolean url = infoObject.getBoolean("url");
info.setUrl(url); JSONArray goodsArray = infoObject.getJSONArray("goods");
List<ShoppingBean.InfoBean.GoodsBean> goodsList = new ArrayList<>();
info.setGood(goodsList); for (int i = 0; i < goodsArray.length(); i++) {
JSONObject goodsObject = goodsArray.getJSONObject(i);
ShoppingBean.InfoBean.GoodsBean goods = new ShoppingBean.InfoBean.GoodsBean(); long addTime = goodsObject.getLong("add_time");
goods.setAdd_time(addTime); String colorCode = goodsObject.getString("colorcode");
goods.setColorcode(colorCode); String currencyPrice = goodsObject.getString("currency_price");
goods.setCurrency_price(currencyPrice); String description = goodsObject.getString("description");
goods.setDescription(description); String goodsName = goodsObject.getString("goods_name");
goods.setGoods_name(goodsName); String thumb = goodsObject.getString("thumb");
goods.setThumb(thumb); goodsList.add(goods);
} } catch (Exception e) {
e.printStackTrace();
} return shopping; } // 读取文件中的JSON数据
private void readFile() {
// 根目录
// Environment.getExternalStorageDirectory() // 外部存储公共路径,例如:DCIM,DOWNLOADS等系统提供的文件夹
// Environment.getExternalStoragePublicDirectory(类型) // 外部存储私有路径:Android文件夹
// context.getExternalFilesDir(null) // downloads文件夹路径
String filePath = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.getAbsolutePath();
String fileName = "json.txt"; File file = new File(filePath, fileName); // 文件字符输入流
// 字缓符输入冲流
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line = new String();
while ((line = br.readLine()) != null) {
mJson += line;
}
if (mJson != null) {
mResultTv.setText(mJson);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} private void initView() {
mReadFileBtn = (Button) findViewById(R.id.read_file_btn);
mReadFileBtn.setOnClickListener(MainActivity.this);
mParseBtn = (Button) findViewById(R.id.parse_btn);
mParseBtn.setOnClickListener(MainActivity.this);
mResultTv = (TextView) findViewById(R.id.result_tv);
}
}
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
原生JSON解析的更多相关文章
- .Net Core 3.0原生Json解析器
微软官方博客中描述了为什么构造了全新的Json解析器而不是继续使用行业准则Json.Net 微软博客地址:https://devblogs.microsoft.com/dotnet/try-the-n ...
- iOS原生JSON解析.
- (IBAction)accessInterfaceBtnPressed:(id)sender { NSError *error; NSString *URL=@"ht ...
- iOS SDK原生JSON解析
- (IBAction)touchReadButton:(id)sender { NSData *jsonData = [[NSData alloc] initWithContentsOfFile:J ...
- iOS中JSON解析三方库的比较
网络数据解析框架 1. JsonModel 一个 JSON 模型转换库,有着比较简洁的接口.Model 需要继承自 JSONModel. 2. yyModel yyModel比较轻量(算上.h 只 ...
- ios基础篇(二十七)—— Json解析
一.什么是Json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使 ...
- IOS中Json解析的四种方法
作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验 ...
- JSON解析方案
在iOS中,JSON的常见解析方案有4种 第三方框架:JSONKit,SBJson,TouchJSON(性能从左到右,越差) 苹果原生(自带):NSJSONSerialization(性能最好) JS ...
- iOS开发之网络数据解析(一)--JSON解析简介
前言:对服务器请求之后,返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) 本篇随便先讲解JSON解析. 正文: 关于JSON: JSON是一种轻量级的数据格式,一般用于数据交互 ...
- Android开源库--Gson谷歌官方json解析库
官方文档地址:http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html 官方网站:http://code.go ...
随机推荐
- 课程二(Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization),第三周(Hyperparameter tuning, Batch Normalization and Programming Frameworks) —— 2.Programming assignments
Tensorflow Welcome to the Tensorflow Tutorial! In this notebook you will learn all the basics of Ten ...
- oc中类的实例化及方法调用
上一篇我们讲了oop和类的创建,上一篇的重点我们回顾一下 类 对象 实例 方法 接口 这一篇我们来实现类的实例化,调用类中的公共参数和方法:类的实现在.m文件中,以下是实现代码: // // HuiT ...
- ring3下的IAT HOOK
标 题: [原创]ring3下的IAT HOOK作 者: hostzhen时 间: 2013-03-28,11:30:53链 接: http://bbs.pediy.com/showthread.ph ...
- 解决tomcat使用时catalina.out过大的问题
在项目上线之前需要做很多事情,其中最容易忘记的就是禁用不必要的日志和调整必要的参数. 调整参数这里主要指的是最大连接数,最小等待时间,虚拟机内存等. 而这里重点要提出的是,你需要禁用tomcat中不必 ...
- CRM项目完成实现
这两天我们都在写项目的接口与实现的! 今天主要是遇到的问题: 当我在添加新的班级的时候,班级的老师 我需要去判断这个老师是否存在,如果不存在的话,我就要把这个老师添加到学校的员工表中! 我会先判断这个 ...
- Django models 的常用字段类型和字段参数
<1> CharField #字符串字段, 用于较短的字符串. #CharField 要求必须有一个参数 maxlength, 用于从数据库层和Django校验层限制该字段所允许的最大字符 ...
- ConcurrentHashmap源码好好给你说明白
这个ConcurrentHashmap的设计非常精妙,如果有疑问的地方,欢迎大家在评论区进行激烈讨论! 一.静态工具方法 private static final int tableSizeFor(i ...
- linux 安装 nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash 或者 wget -qO- htt ...
- 百度前端技术学院-task1.8源代码以及个人总结
通过这次任务的练习我学到了很多,现将所学到的以及遇到的问题总结如下. 源码地址:http://pan.baidu.com/s/1kVB2VZL 下面出现的代码,都是摘自这个上面的源码 1.终于明白了a ...
- 并发编程——详解 AQS CLH 锁
从 acquire 方法开始 -- 获取 为什么 AQS 需要一个虚拟 head 节点 reelase 方法如何释放锁 总结 前言 AQS 是 JUC 中的核心,其中封装了资源的获取和释放,在我们之前 ...