Android的JSON数据解析
一、 使用原生方式解析
准备工作:准备一个布局文件,用来显示元数据与转换之后的数据
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btn_String2JOSNObject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用原生方式:String2JOSNObject " /> <Button
android:id="@+id/btn_String2JOSNArray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用原生方式:String2JOSNArray " /> <Button
android:id="@+id/btn_String2Bean_Gson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用Gson方式:String2Bean " /> <Button
android:id="@+id/btn_String2List_Gson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用Gson方式:String2List " /> <Button
android:id="@+id/btn_String2Arrays_Gson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用Gson方式:String2Arrays " /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="元数据"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp" /> <TextView
android:id="@+id/tv_metadata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="转换后的数据"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp" /> <TextView
android:id="@+id/tv_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" /> </LinearLayout>
1. 使用原生方式---将String转换为JOSNObject
1.1 准备元数据
//使用原生方式: JSONObject2String
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("name","小米");
jsonObject.put("age",1);
JSONObject familyJOSN = new JSONObject();
familyJOSN.put("father","米爸");
familyJOSN.put("mother","米妈");
jsonObject.put("family",familyJOSN);
JSONArray hobbyArray = new JSONArray();
hobbyArray.put("篮球");
hobbyArray.put("看书");
jsonObject.put("hobby",hobbyArray); } catch (JSONException e) {
e.printStackTrace();
}
metaDataStr = jsonObject.toString();
tv_metadata.setText(jsonObject.toString());
1.2 开始转换数据
//使用原生方式:String2JOSNObject
findViewById(R.id.btn_String2JOSNObject).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
JSONObject jsonObject = new JSONObject(metaDataStr);
Log.e(JOSNActivity.class.getName(),jsonObject.toString());
String name = jsonObject.getString("name");
String age = jsonObject.getString("age");
JSONObject familyJOSN = new JSONObject(String.valueOf(jsonObject.getJSONObject("family")));
String father = familyJOSN.getString("father");
String mother = familyJOSN.getString("mother");
JSONArray hobbyArray = new JSONArray(jsonObject.getJSONArray("hobby").toString());
String hobby = "";
for(int i = 0; i < hobbyArray.length(); i ++){
if(i != hobbyArray.length() -1){
hobby = hobby + (String) hobbyArray.get(i)+"、";
}else {
hobby = hobby + (String) hobbyArray.get(i);
} }
String resultStr = "姓名:"+name+",年龄:"+age+",家庭成员:"+father+"、"+mother+",爱好:"+hobby;
tv_data.setText(resultStr);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
2. 使用原生方式:将String转换为JOSNArray
2.1 准备元数据
// JSONArray2String
JSONArray array = new JSONArray();
for(int i = 0; i < 2; i ++){
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("name","小米"+i);
jsonObject.put("age",22+i);
array.put(jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
}
metaDataStr = array.toString();
tv_metadata.setText(metaDataStr);
2.2 开始转换数据
//使用原生方式:String2JOSNArray
findViewById(R.id.btn_String2JOSNArray).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String resultStr = "";
JSONArray jsonArray = new JSONArray(metaDataStr);
for(int i = 0; i < jsonArray.length(); i ++){
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
resultStr = resultStr + "第" + i + "组姓名:"+ jsonObject.getString("name") +
",年龄:"+ jsonObject.getString("age")+"\n";
}
tv_data.setText(resultStr);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
二、 使用GSON解析JSON数据
1. 使用Gson方式:将String转换为Bean
1.1 准备Bean类
static class Bean{ public Bean(String name,int age,List<String> hobby){
this.name = name;
this.age = age;
this.hobby = hobby;
Family family = new Family();
family.setFather(name+"爸");
family.setMother(name+"妈");
this.family = family;
} /**
* name : 小米
* family : {"mother":"米妈","father":"米爸"}
* age : 1
* hobby : ["篮球","看书"]
*/
private String name;
private Family family;
private int age;
private List<String> hobby; public void setName(String name) {
this.name = name;
} public void setFamily(Family family) {
this.family = family;
} public void setAge(int age) {
this.age = age;
} public void setHobby(List<String> hobby) {
this.hobby = hobby;
} public String getName() {
return name;
} public Family getFamily() {
return family;
} public int getAge() {
return age;
} public List<String> getHobby() {
return hobby;
} public static class Family {
/**
* mother : 米妈
* father : 米爸
*/
private String mother;
private String father; public void setMother(String mother) {
this.mother = mother;
} public void setFather(String father) {
this.father = father;
} public String getMother() {
return mother;
} public String getFather() {
return father;
}
}
}
1.2 准备元数据
//使用Gson方式:Bean2String
Gson gson = new Gson();
List<String> list = new ArrayList<>();
list.add("篮球");
list.add("看书");
Bean bean = new Bean("小米",23,list);
metaDataStr = gson.toJson(bean).toString();
tv_metadata.setText(metaDataStr);
1.3 开始转换数据
//使用Gson方式:String2Bean
findViewById(R.id.btn_String2Bean_Gson).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Gson gson = new Gson();
Bean resultBean = gson.fromJson(metaDataStr,Bean.class);
String name = resultBean.getName();
int age = resultBean.getAge();
String father = resultBean.getFamily().getFather();
String mother = resultBean.getFamily().getMother();
List<String> list = resultBean.getHobby();
String hobby = "";
for(int i = 0; i < list.size(); i ++){
if(i != list.size() -1){
hobby = hobby + (String) list.get(i)+"、";
}else {
hobby = hobby + (String) list.get(i);
} }
String resultStr = "姓名:"+name+",年龄:"+age+",家庭成员:"+father+"、"+mother+",爱好:"+hobby;
tv_data.setText(resultStr);
}
});
2. 使用Gson方式:将String转换为List
2.1 准备元数据
//使用Gson方式:List2String
Gson gson = new Gson();
List<Bean> beans = new ArrayList<>();
List<String> list = new ArrayList<>();
list.add("篮球");
list.add("看书");
Bean bean1 = new Bean("小米",23,list);
Bean bean2 = new Bean("菲菲",24,list);
beans.add(bean1);
beans.add(bean2);
metaDataStr = gson.toJson(beans);
tv_metadata.setText(metaDataStr);
2.2 开始转换数据
//使用Gson方式:String2List
findViewById(R.id.btn_String2List_Gson).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Gson gson = new Gson();
// Gson为我们提供了TypeToken来实现对泛型的支持,
List<Bean> beans = gson.fromJson(metaDataStr,new TypeToken<List<Bean>>(){}.getType());
String resultStr ="";
for(int i = 0; i < beans.size() ; i++){
String name = beans.get(i).getName();
int age = beans.get(i).getAge();
String father = beans.get(i).getFamily().getFather();
String mother = beans.get(i).getFamily().getMother();
resultStr = resultStr + "姓名:"+name+",年龄:"+age+",家庭成员:"+father+"、"+mother+"\n";
}
tv_data.setText(resultStr);
}
});
3. 使用Gson方式:将String转换为Arrays
3.1 准备元数据
//使用Gson方式:Arrays2String
String[] arrays = {"张三","李四","王五","赵六"};
Gson gson = new Gson();
metaDataStr = gson.toJson(arrays);
tv_metadata.setText(metaDataStr);
3.2 开始转换数据
//使用Gson方式:String2Arrays
findViewById(R.id.btn_String2Arrays_Gson).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Gson gson = new Gson();
//此时String[]当成一个实体类
String[] arrays = gson.fromJson(metaDataStr,String[].class);
String resultStr = "";
for(int i = 0; i < arrays.length;i ++){
resultStr = resultStr + arrays[i] +";";
}
tv_data.setText(resultStr);
}
});
Android的JSON数据解析的更多相关文章
- Android系列---JSON数据解析
您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...
- Android 实现Json数据解析,并进行应用!
从网站上获取数据然后再客户端进行解析是常见的数据交互.下面是常用的一些接口网址: webservice工厂接口 http://www.36wu.com 快递查询接口http://webservice. ...
- Android 之JSON数据解析
(以下基本都是郭霖大神<第一行代码>中的知识) JSON数据与xml相比,优势在于体积更小,传输所需的流量少.但是缺点也很明显,就是语义性较差. 下面是一组JSON格式的数据. [{&qu ...
- Android关于JSON数据解析
一.什么是json json(Javascript Object Notation)是一种轻量级的数据交换格式,相比于xml这种数据交换格式来说,因为解析xml比较的复杂,而且需要编写大段的代码,所以 ...
- Android下Json数据解析
如从网络获取JSON 则需要创建一个工具类,该类返回一个字符串为JSON文本 package com.example.jsonapp; import java.io.InputStreamReader ...
- Android 之 json数据的解析(jsonReader)
json数据的解析相对而言,还是比较容易的,实现的代码也十分简单.这里用的是jsonReade方法来进行json数据解析. 1.在解析之前,大家需要知道什么是json数据. json数据存储的对象是无 ...
- Android网络之数据解析----使用Google Gson解析Json数据
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- Android JSON数据解析(GSON方式)
要创建和解析JSON数据,也可以使用GSON来完成.GSON是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库.使用GSON,可以很容易的将一串JSON数据转换为一个Jav ...
随机推荐
- jdbc连接oracle时报错 Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableC
错误: Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; ...
- viewport的故事(一)
部分翻译 自原文 https://www.quirksmode.org/mobile/viewports.html 概念:设备像素和CSS像素 设备像素可以通过 screen.width/he ...
- Oracle SQL 内置函数大全(转)
SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数;SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ...
- Python练手例子(3)
13.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153=1 ...
- java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec
java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec 这个类在 rt.jar 里面 本地开发,jre里有这个包,所以不会 ...
- jsplumb 使用总结
1 删除连线问题 funcion clearDrawGraph { if (this.graphInstance !== null) { const connections = this.graphI ...
- 14 - How to check replication status
The people using PostgreSQL and the Streaming Replication feature seem to ask many of the same quest ...
- Mac OSX bash function 备份
# mount the android file image function mountAndroid { hdiutil attach ~/android.dmg.sparsefile.spars ...
- Windows Server 2012 R2服务器部署Tomcat JDK、安装Mysql以及将Java项目部署到CVM
我们平时所调试的Java Web 项目需要在本地Eclipse或者MyEclipse当中开发调试,并且部署到Tomcat上来测试,比如说笔者这里用的eclipse添加tomcat服务器, 但是这里发布 ...
- Python 写了个小程序,耗时一天,结果才100多行
from selenium import webdriver import selenium.webdriver.support.ui as ui from selenium.webdriver.co ...