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

页面导航:首页 > 网络编程 > JavaScript > Android JSON数据的解析与封装小Demo

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

来源: 作者: 时间:2016-02-05 11:06 【

今天早早的下班了,写了一个简单的JSON Demo,这里总结一下。首先是效果图:简单起见,解析部分并没有采用解析URL的方式,直接把JSON写到String里,逻辑比较简单,这里就不啰嗦了,上

今天早早的下班了,写了一个简单的JSON Demo,这里总结一下。

首先是效果图:

\


<喎"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+vPK1pcbwvPujrL3izvayv7fWsqLDu9PQssnTw73izvZVUky1xLe9yr2jrNaxvdOw0UpTT07QtLW9U3RyaW5nwO+jrMLfvK2xyL3PvPK1paOs1eLA777NsruGquDCwcujrMnPtPrC66O6PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">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(); } } }


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




    

    

    

        

        

        


转载请注明出处:周木水的CSDN博客 http://blog.csdn.net/zhoumushui

我的GitHub:周木水的GitHub https://github.com/zhoumushui


Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<