Android学习之JSON数据解析
在Android应用开发中,常用的数据交换格式有XML和JSON,这两种方式各有各的好处,我们在特定的应用开发中可以选择合适的一种。下面来看一下JOSN数据解析:
例子永远是最好的教程,下面我们来看个例子!
有这样一个JSON数据:"{"username":"zhangsan","password":"123456"}"
通过解析后对应的数据显示在相应的控件中:
就是上面这种效果。
在Android中使用json需要一个jar包,gson-1.7.jar;可以在google的网站上下载。把这个包加到项目的构建路径中就行了。
下面是这个项目的源码(源码中的类及方法可以参考API文档):
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gufengxiachen"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".json"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/username"
/>
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/password"
/>
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></EditText>
<Button
android:id="@+id/parse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/parse"
>
</Button>
</LinearLayout>
package com.gufengxiachen;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class json extends Activity {
/** Called when the activity is first created. */
private String name;
private String ages;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAges() {
return ages;
}
public void setAges(String ages) {
this.ages = ages;
}
private EditText username=null;
private EditText password=null;
private Button parse=null;
private String jsonData = "[{\"username\":\"zhagnsan\",\"password\":\"123456\"}]";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
parse = (Button)findViewById(R.id.parse);
parse.setOnClickListener(new parseListener());
}
public class parseListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ParseJson parseJson = new ParseJson();
json json = parseJson.parse(jsonData);
username.setText(json.getName());
password.setText(json.getAges());
}
}
}
package com.gufengxiachen;
import java.io.IOException;
import java.io.StringReader;
import com.google.gson.stream.JsonReader;
public class ParseJson {
public json parse(String jsonData){
json json =new json();
JsonReader jsonReader = new JsonReader(new StringReader(jsonData));
try {
jsonReader.beginArray();
while(jsonReader.hasNext()){
jsonReader.beginObject();
while(jsonReader.hasNext()){
String tagName = jsonReader.nextName();
if(tagName.equals("username")){
json.setName(jsonReader.nextString());
System.out.println(json.getName());
}else if(tagName.equals("password")){
json.setAges(""+jsonReader.nextInt());
System.out.println(json.getAges());
}
}
jsonReader.endObject();
}
jsonReader.endArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
}
转载于:https://blog.51cto.com/wustrive2008/599577
Android学习之JSON数据解析相关推荐
- json数据解析 android,Android基础之json数据解析
这次来聊聊Android中json数据的解析,既然是基础就要求只要你看一遍就必须会的,不牵涉到太多的复杂逻辑,目的就是让你初步认识json数据的解析和学会对简单的json数据进行解析. 首先第一步我们 ...
- Kotlin学习之json数据解析
通过这篇文章,可以学习到 1. 如何根据json数据创建一个对应的数据类 2. 如何根据json数据创建一个对象 (一)根据json数据创建一个对应的数据类 data class Test(val c ...
- Android学习笔记44:JSON数据解析
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 本文将主要介绍在Android ...
- Android JSON数据解析(GSON方式)
要创建和解析JSON数据,也可以使用GSON来完成.GSON是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库.使用GSON,可以很容易的将一串JSON数据转换为一个Jav ...
- Android json数据解析及简单例子
JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...
- Android系列---JSON数据解析
http://www.cnblogs.com/xiaoluo501395377/p/3446605.html Android系列---JSON数据解析 转载于:https://www.cnblogs. ...
- linux下json数据解析,JSON数据解析 - iOS学习笔记_Linux编程_Linux公社-Linux系统门户网站...
在之前的<iOS学习--xml数据解析(九)>http://www.linuxidc.com/Linux/2014-02/97020p9.htm 介绍了xml数据解析,这一篇简单介绍一下J ...
- android json解析异常,json数据解析异常而导致网络请求失败的解决办法(其一)
问题概述 笔者在开发过程中临时遇到一个本来仅有web端的项目临时增加Android端,导致后端在出接口时并未考虑Android端的json数据的解析,导致接口是这样的.... 正确请求 { " ...
- android studio json数据解析汇总(备忘)
之前写过一个,但是写不太全,结果自己都看不懂,希望这个以后能看懂吧. 以两个例子说明json数据解析,一个是最简单的,一个是比较难的. 必须知道的:JSONObject和JSONArray JSONO ...
最新文章
- 设计模式(八): 从“小弟”中来类比外观模式(Facade Pattern)
- 【快乐水题】677. 键值映射
- vs2008 使用Visual Leak Detector检测内存泄漏
- Ubuntu + pip + Python3 + CPU版本 PaddlePaddle 安装
- flume高可用-failover-模型分析
- Apache骆驼丝攻示例
- inputstreamreader 编码不完整_素材编码对剪辑效率的影响
- 解决企业子CA无法检查吊销的问题
- Luogu2014选课
- Win10验证USB Audio MIC(三)
- Windows 8家长控制
- java中的字符串处理
- 北大教授的美国博士生涯:学术是自由灵魂的分泌物
- java编程英语词汇_Java编程基础常见英语词汇
- 南京卡地亚数码科技 网站维护 数据更新
- 学数学计算机考研,计算机考研考数学
- 零基础入门,花生壳骨灰级微信小程序开发教程
- 下载tensorflow时遇到的问题
- 华为EMUI屏蔽更新
- ffmpeg下载安装的步骤