转自:http://www.open-open.com/lib/view/open1420529336406.html
package com.example.jsondemo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {

// private static final String BASE_URL =
// "http://zhoumushui.sinaapp.com/json/";
private TextView tvMsg;
private TextView tvJson;
private EditText etName;
private EditText etAge;
private String strJson = "";
private String staffInfo = "";
private String strJsonRes = "";
private String strMsg;
private Button btnAdd;
private Button btnJson;

@Override
public void onCreate(Bundle savedInstanceState) {
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
}

class onClickListenerImp implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
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, "Add Succcess!", Toast.LENGTH_SHORT)
.show();
etAge.setText("");
etName.setText("");
} else if (v == btnJson) {
strJsonRes = "";
strJsonRes = strMsg + "]}";
tvJson.setText(strJsonRes);
}
}
}

private void MsgToJson() {
btnAdd.setOnClickListener(new onClickListenerImp());
btnJson.setOnClickListener(new onClickListenerImp());

}

private void JsonToMsg() {
strJson = "{staff:[{name:'Alex',age:21},{name:'Zhou',age:22},{name:'Anne',age:23}],company:'T-Chip'}";
staffInfo = "原始数据:\n" + strJson + "\n\n解析之后:\n";
try {
JSONObject mJsonObject = new JSONObject(strJson);

JSONArray mJsonArray = mJsonObject.getJSONArray("staff");
String company = mJsonObject.getString("company");
staffInfo = staffInfo + company + "共有 " + mJsonArray.length()
+ " 个员工,信息如下:\n";

for (int staffCount = 0; staffCount < mJsonArray.length(); staffCount++) {
// 获取员工
JSONObject staff = mJsonArray.getJSONObject(staffCount);
int staffNo = staffCount + 1;
staffInfo = staffInfo + "序号:" + staffNo + " 姓名: "
+ staff.getString("name") + " 年龄: "
+ staff.getInt("age") + "\n";
}
tvMsg.setText(staffInfo);
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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>

更多相关文章

  1. android根据姓名首字母字符顺序读取通信录
  2. 电话接听并查看该人姓名
  3. Android Studio(4)---开发人员工作流程基础
  4. Android根据联系人姓名首字符顺序读取通讯录
  5. 比如说我们要从用户表customer和用户订单表orders中,查询上海的用
  6. 员工要离职,老板画饼,饼太大消化不了
  7. 百度账上躺着1300亿,今年员工将大幅涨薪,BAT回来了?
  8. 公司逼走员工的几个套路,你有中招吗?
  9. 神州优车员工爆料:去年工位宣读裁员的HR也被裁

随机推荐

  1. Android(安卓)RxJava 实际应用讲解:联想搜
  2. 15个Android开源库
  3. Android(安卓)Studio 文件提前结束
  4. Android理解 Window 与 WindowManager
  5. Android(安卓)Gradle构建-理解DSL语言以
  6. Android(安卓)笔记:获取本机手机号(适用于
  7. 《Android和PHP开发最佳实践》一2.1 And
  8. Android(安卓)Fragment 解析
  9. Android(安卓)OkHttp完全解析 是时候来了
  10. Android内存优化全解析