在Android應用開發中,經常使用的數據交換格式有XML和JSON,這兩種方式各有各的好處,咱們在特定的應用開發中能夠選擇合適的一種。下面來看一下JOSN數據解析:java
例子永遠是最好的教程,下面咱們來看個例子!android
有這樣一個JSON數據:"{"username":"zhangsan","password":"123456"}"web
經過解析後對應的數據顯示在相應的控件中:json
就是上面這種效果。app
在Android中使用json須要一個jar包,gson-1.7.jar;能夠在google的網站上下載。把這個包加到項目的構建路徑中就好了。ide
下面是這個項目的源碼(源碼中的類及方法能夠參考API文檔):網站
AndroidManifest.xml:this
<?xml version="1.0" encoding="utf-8"?>google
<manifest xmlns:android="http://schemas.android.com/apk/res/android"spa
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;
}
}