json数据解析并显示在listView上

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;
    }
}


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值