`
chengfengyang
  • 浏览: 21122 次
社区版块
存档分类
最新评论

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>

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;

}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics