json解析
布局文件:
<?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=">>>>>>>>>>"
/>
<TextView
android:text="username:"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editText1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="textPersonName"/>
<TextView
android:text="password:"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editText2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="textPersonName"/>
<Button
android:text="获取数据"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
代码:
package com.amaker.app;
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 JSONTestActivity extends Activity {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setAges(String pwd) {
this.pwd = pwd;
}
private EditText username=null;
private EditText password=null;
private Button parse=null;
private String jsonData = "[{\"username\":\"zhangsan(张三)\",\"password\":\"123456\"}]";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
parse = (Button)findViewById(R.id.button1);
parse.setOnClickListener(new parseListener());
}
public class parseListener implements OnClickListener{
@Override
public void onClick(View v) {
ParseJson parseJson = new ParseJson();
JSONTestActivity json = parseJson.parse(jsonData);
username.setText(json.getName());
password.setText(json.getPwd());
}
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>
package com.amaker.app;
import java.io.IOException;
import java.io.StringReader;
import com.google.gson.stream.JsonReader;
public class ParseJson {
public JSONTestActivity parse(String jsonData){
JSONTestActivity json =new JSONTestActivity();
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.getPwd());
}
}
jsonReader.endObject();
}
jsonReader.endArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
}
效果如下
详细请看:http://hi.baidu.com/wustrive_2008/blog/item/f4c8e9877435ea2767096e59.html