Android JSON数据的解析与封装小Demo

Urchindong 2015-03-26

转自:http://www.open-open.com/lib/view/open1420529336406.html

packagecom.example.jsondemo;

importorg.json.JSONArray;

importorg.json.JSONException;

importorg.json.JSONObject;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.Menu;

importandroid.view.MenuItem;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.EditText;

importandroid.widget.TextView;

importandroid.widget.Toast;

publicclassMainextendsActivity{

//privatestaticfinalStringBASE_URL=

//"http://zhoumushui.sinaapp.com/json/";

privateTextViewtvMsg;

privateTextViewtvJson;

privateEditTextetName;

privateEditTextetAge;

privateStringstrJson="";

privateStringstaffInfo="";

privateStringstrJsonRes="";

privateStringstrMsg;

privateButtonbtnAdd;

privateButtonbtnJson;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tvMsg=(TextView)findViewById(R.id.tvMsg);

tvJson=(TextView)findViewById(R.id.tvJson);

etName=(EditText)findViewById(R.id.etName);

etAge=(EditText)findViewById(R.id.etAge);

btnAdd=(Button)findViewById(R.id.btnAdd);

btnJson=(Button)findViewById(R.id.btnJson);

MsgToJson();//封装Json

JsonToMsg();//解析Json

}

classonClickListenerImpimplementsOnClickListener{

@Override

publicvoidonClick(Viewv){

//TODOAuto-generatedmethodstub

if(v==btnAdd){

if(strMsg!=null&&strJson.trim().length()!=0){

strMsg=strMsg+",{name:'"+etName.getText().toString()

+"',age:"+etAge.getText().toString()+"}";

}else{

strMsg="{staff:[{name:'"+etName.getText().toString()

+"',age:"+etAge.getText().toString()+"}";

}

Toast.makeText(Main.this,"AddSucccess!",Toast.LENGTH_SHORT)

.show();

etAge.setText("");

etName.setText("");

}elseif(v==btnJson){

strJsonRes="";

strJsonRes=strMsg+"]}";

tvJson.setText(strJsonRes);

}

}

}

privatevoidMsgToJson(){

btnAdd.setOnClickListener(newonClickListenerImp());

btnJson.setOnClickListener(newonClickListenerImp());

}

privatevoidJsonToMsg(){

strJson="{staff:[{name:'Alex',age:21},{name:'Zhou',age:22},{name:'Anne',age:23}],company:'T-Chip'}";

staffInfo="原始数据:\n"+strJson+"\n\n解析之后:\n";

try{

JSONObjectmJsonObject=newJSONObject(strJson);

JSONArraymJsonArray=mJsonObject.getJSONArray("staff");

Stringcompany=mJsonObject.getString("company");

staffInfo=staffInfo+company+"共有"+mJsonArray.length()

+"个员工,信息如下:\n";

for(intstaffCount=0;staffCount<mJsonArray.length();staffCount++){

//获取员工

JSONObjectstaff=mJsonArray.getJSONObject(staffCount);

intstaffNo=staffCount+1;

staffInfo=staffInfo+"序号:"+staffNo+"姓名:"

+staff.getString("name")+"年龄:"

+staff.getInt("age")+"\n";

}

tvMsg.setText(staffInfo);

}catch(JSONExceptione){

e.printStackTrace();

}catch(Exceptione){

e.printStackTrace();

}

}

}

布局有点拖沓,其实数据封装部分还可以利用一下解析部分的逻辑。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

<TextView

android:id="@+id/tvMsg"

android:layout_width="fill_parent"

android:layout_height="wrap_content"/>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="=========================="/>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<EditText

android:id="@+id/etName"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="姓名"/>

<EditText

android:id="@+id/etAge"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="年龄"/>

<Button

android:id="@+id/btnAdd"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="增加"/>

<Button

android:id="@+id/btnJson"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="JSON"/>

</LinearLayout>

<TextView

android:id="@+id/tvJson"

android:layout_width="fill_parent"

android:layout_height="wrap_content"/>

</LinearLayout>

相关推荐

大树 / 0评论 2011-09-29