json数据解析并显示在listView上
2016-03-23 09:17
489 查看
package com.example.josn_test; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; //{ // "_id": "56e7d93c531da44310cbcd73", // "goodsname": "爱家庭-少儿健康保障B计划", // "shelfstate": "1", // "goodstype": "意健险", // "age": "7周岁-17周岁", // "planners": [ // { // "coverage": "5万元", // "name": "意外伤害保险金" // }, // { // "coverage": "5万元", // "name": "疾病身故保险金" // }, // { // "coverage": "10万元", // "name": "重大疾病保险金" // }, // { // "coverage": "3.5万元", // "name": "住院医疗费用补偿" // } // ] //} public class MainActivity extends Activity { private ListView lv; private ArrayList<String> list_string; private MyPagerAdater myPagerAdater; private String json = "{" + " \"_id\": \"56e7d93c531da44310cbcd73\", " + " \"goodsname\": \"爱家庭-少儿健康保障B计划\", " + " \"shelfstate\": \"1\", " + " \"goodstype\": \"意健险\", " + " \"age\": \"7周岁-17周岁\", " + " \"planners\": [ " + " { " + " \"coverage\": \"5万元\", " + " \"name\": \"意外伤害保险金\" " + " }, " + " { " + " \"coverage\": \"5万元\", " + " \"name\": \"疾病身故保险金\" " + " }, " + " { " + " \"coverage\": \"10万元\", " + " \"name\": \"重大疾病保险金\" " + " }, " + " { " + " \"coverage\": \"3.5万元\", " + " \"name\": \"住院医疗费用补偿\" " + " } " + "] " + " }"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); jsonTest(); myPagerAdater = new MyPagerAdater(); lv.setAdapter(myPagerAdater); } private void jsonTest() { try { JSONObject jsonObject = new JSONObject(json); String id = jsonObject.getString("_id"); String goods_name = jsonObject.getString("goodsname"); String shelfstate = jsonObject.getString("shelfstate"); String goodstype = jsonObject.getString("goodstype"); String age = jsonObject.getString("age"); JSONArray jsonArray = jsonObject.getJSONArray("planners"); list_string = new ArrayList<>(); list_string.add(id); list_string.add(goods_name); list_string.add(shelfstate); list_string.add(goodstype); list_string.add(age); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject2 = (JSONObject) jsonArray.get(i); String coverage = jsonObject2.getString("coverage"); String name = jsonObject2.getString("name"); list_string.add(coverage); list_string.add(name); } } catch (JSONException e) { e.printStackTrace(); } } public class MyPagerAdater extends BaseAdapter { @Override public int getCount() { return list_string.size(); } @Override public Object getItem(int position) { return list_string.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { viewHolder = new ViewHolder(); convertView = getLayoutInflater().inflate(R.layout.item, null); viewHolder.textView = (TextView) convertView.findViewById(R.id.item); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.textView.setText(list_string.get(position)); return convertView; } } final static class ViewHolder { TextView textView; } }
相关文章推荐
- JavaScript 面向对象编程入门案例
- JS随笔-HTML5图片轮播
- js性能优化
- 因 Nodo.js 公司独断转移了模块所有权,模块开发者怒而撤下了他的所有模块
- js 节流函数 throttle
- JavaScript Array模拟C#list RemoveAll实现删除符合条件的元素
- OBIEE中JS的使用-----在分析列中添加图片
- Jsp与Servlet 重定向与转发探究(二)
- OBIEE中JS的使用--在分析列中添加链接
- 清除WebSphere部署应用所对应的JSP缓存
- 控制表单内容字段必填项的一种设计思路
- OBIEE中JS的使用 ---- 视图选择器居左
- 随笔:弱类型语言javascript中的一些小坑
- JavaScript学习笔记之数组的增、删、改、查
- JavaScript学习笔记之数组求和方法
- JavaScript学习笔记之数组随机排序
- JavaScript学习笔记之数组去重
- JavaScript学习笔记之取数组中最大值和最小值
- JS随机洗牌算法之数组随机排序
- JavaScript弹出对话框的三种方式