在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>
json.java:
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());
}
}
}
ParseJson.java:
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;
}
}
分享到:
相关推荐
Android Json 解析demo 建立,基于Android stdio
android 项目开发实例,json 解析参考, 希望对你们有用
android json解析demo,简单粗暴
android json 解析jar包。没有积分可以取GitHub上下载
Android_JSON数据解析 博客里面的Demo
Android json解析jar包,挺方便的解析jar包。
AndroidJSON解析框架GSON,可以方便的解析JSON,提高效率,欢迎大家下载使用。
Android JSON解析示例代码.txt
android JSon解析例子,希望可以帮助大家。
android框架解析JSON数据放入ListView的例子
Android JSON 解析库的使用 里面包括详细的文档和代码 其中fastjson和gson的对比以及示例代码
Android 与后台交互,Json数据传输解析
android json 解析 fastjson。
通过编写配置文件,该代码生成器能自动生成相应的bean,和解析json的代码。 你需要做的是,有一个ruby环境,将配置...生成json解析代码: ruby json_parser.rb > JSONParser.java 不使用重定向,模式是打印在终端中。
Android JSON解析
eclipse项目。获取raw文件下的json文件。无需编写json数据里面key值的实体类,动态获取里面的键值对的值。并在列表显示
笔者近期做到对天气预报JSON数据解析,在此小记。 天气预报接口:http://wthrcdn.etouch.cn/weather_mini?citykey=101200101 JSON数据如下: { desc: OK, status: 1000, data: { wendu: 14, ganmao: 天气...
我们通常在Android上采用Gson来解析Json数据,很方便的就可以把数据转换成List或者map。当碰到日期时间的时候,你可能会遇到坑,需要格式化一下日期时间格式。 本文介绍org.json这种不需要导入任何包的解析方式,...
android移动应用的json解析数据,解析对象和数组
android json数据下载与解析demo,实现了http下载json服务器数据,并通过JsonObject解析json数据