2 qq 33720124 qq_33720124 于 2016.02.22 10:39 提问

Android这样的JSON数据应该怎么解析。

图片说明

这是一个选择添加疾病的页面。
第一行是疾病类型
第二行是疾病名称
要求上面的疾病类型和疾病名称都要从服务器获取。
每种疾病类型下面的疾病名称数量不确定

还有可以点击选择添加日期,不过这个问题我已经解决了。
我采用的是ListView展示的。但是解析过后有问题,只能解析到疾病类型,解析不到疾病名称。

每个疾病类型和疾病名称都有ID。
这是用网页访问JSP网址的返回值。
{"code":200,"message":"调用成功","data":{"mem_message_records":[{"message_disease_typeid":"1","message_disease_type":"心血管系统"},{"message_disease_typeid":"2","message_disease_type":"外围血管疾病"},{"message_disease_typeid":"3","message_disease_type":"脑血管疾病"},{"message_disease_typeid":"4","message_disease_type":"肺部疾病"},{"message_disease_typeid":"5","message_disease_type":"代谢疾病"},{"message_disease_typeid":"6","message_disease_type":"周围神经疾病"},{"message_disease_typeid":"7","message_disease_type":"其他疾病"}],"mem_message_recordss":[[{"message_diseaseid":"1","message_disease":"冠状动脉疾病"},{"message_diseaseid":"2","message_disease":"心绞痛"},{"message_diseaseid":"3","message_disease":"心肌梗死"},{"message_diseaseid":"4","message_disease":"冠状动脉血运重建"},{"message_diseaseid":"5","message_disease":"充血性心力衰竭"}],[{"message_diseaseid":"6","message_disease":"闭塞性周围动脉粥样硬化"},{"message_diseaseid":"7","message_disease":"血栓行静脉炎"}],[{"message_diseaseid":"8","message_disease":"脑血栓"},{"message_diseaseid":"9","message_disease":"短暂性脑供血不足"},{"message_diseaseid":"10","message_disease":"脑出血"}],[{"message_diseaseid":"11","message_disease":"慢性支气管炎"},{"message_diseaseid":"12","message_disease":"慢性阻塞性肺病"},{"message_diseaseid":"13","message_disease":"支气管哮喘"},{"message_diseaseid":"14","message_disease":"间质性肺病"},{"message_diseaseid":"15","message_disease":"囊性肺纤维化"}],[{"message_diseaseid":"16","message_disease":"1型糖尿病"},{"message_diseaseid":"17","message_disease":"2型糖尿病"},{"message_diseaseid":"18","message_disease":"糖尿病肾病"}],[{"message_diseaseid":"19","message_disease":"糖尿病性周围精神病"},{"message_diseaseid":"20","message_disease":"自主神经性病变"}],[{"message_diseaseid":"21","message_disease":"高血压"},{"message_diseaseid":"22","message_disease":"视网膜病变"},{"message_diseaseid":"23","message_disease":"恶性肿瘤"},{"message_diseaseid":"24","message_disease":"脑卒中"},{"message_diseaseid":"25","message_disease":"重性精神疾病"},{"message_diseaseid":"26","message_disease":"结核病"},{"message_diseaseid":"27","message_disease":"肝炎"},{"message_diseaseid":"28","message_disease":"先天畸形"},{"message_diseaseid":"29","message_disease":"其它"}]]}}

13个回答

bdmh
bdmh   Ds   Rxr 2016.02.22 10:39
已采纳

写一个类,结构和json格式对应,用gson直接转为对象,对于不定数量的用List就行了

qq_33720124
qq_33720124 我在这边添加了一张图。就是我现在解析得到的结果,
接近 2 年之前 回复
qq_33720124
qq_33720124 实体类我已经写好了。也能得到服务器端返回成功,可是我本地解析得到的数据,只能得到第一行的疾病类型,下面的那些疾病名称却是NULL值。(比如, 我已经解析得到了心血管系统,这是疾病类型,却得不到冠状动脉疾病,这是疾病名称,) 不只是疾病名称是不固定的,疾病类型也是不固定的。
接近 2 年之前 回复
zhouxin_xuan
zhouxin_xuan   2016.02.23 10:55

楼主做出来了吗? 我试着解析了下 结果出来了 不知道是不是你想要的效果

qq_33720124
qq_33720124 好的。收到。谢谢。我看一下。非常感谢。
接近 2 年之前 回复
zhouxin_xuan
zhouxin_xuan 回复密大林: 你看下qq邮箱,看能否用的到
接近 2 年之前 回复
zhouxin_xuan
zhouxin_xuan 回复密大林: 直接拿上面的json数据解析的。
接近 2 年之前 回复
qq_33720124
qq_33720124 15950571521@163.com,我的邮箱。
接近 2 年之前 回复
qq_33720124
qq_33720124 3069115981,我的QQ号。
接近 2 年之前 回复
qq_33720124
qq_33720124 直接解析?服务器返回的是什么。能够获取到全部的数据吗。还没有做出来呢
接近 2 年之前 回复
zhouxin_xuan
zhouxin_xuan 不用gson 直接解析
接近 2 年之前 回复
menglele1314
menglele1314   2016.03.03 15:27

三行代码就解决的事,有必要搞的那么复杂吗?

menglele1314
menglele1314   2016.03.03 15:32

java.lang.reflect.Type type = new TypeToken() {
}.getType();
Bean msg = gson.fromJson(jsonStr, type);

package com.example.itps.myapplication;

import java.util.List;

public class Bean {

private int code;
private String message;
private DataEntity data;

public void setCode(int code) {
this.code = code;
}

public void setMessage(String message) {
this.message = message;
}

public void setData(DataEntity data) {
this.data = data;
}

public int getCode() {
return code;
}

public String getMessage() {
return message;
}

public DataEntity getData() {
return data;
}

public static class DataEntity {
/**
* message_disease_typeid : 1
* message_disease_type : 心血管系统
*/

private List<MemMessageRecordsEntity> mem_message_records;
/**
 * message_diseaseid : 1
 * message_disease : 冠状动脉疾病
 */

private List<List<MemMessageRecordssEntity>> mem_message_recordss;

public void setMem_message_records(List<MemMessageRecordsEntity> mem_message_records) {
    this.mem_message_records = mem_message_records;
}

public void setMem_message_recordss(List<List<MemMessageRecordssEntity>> mem_message_recordss) {
    this.mem_message_recordss = mem_message_recordss;
}

public List<MemMessageRecordsEntity> getMem_message_records() {
    return mem_message_records;
}

public List<List<MemMessageRecordssEntity>> getMem_message_recordss() {
    return mem_message_recordss;
}

public static class MemMessageRecordsEntity {
    private String message_disease_typeid;
    private String message_disease_type;

    public void setMessage_disease_typeid(String message_disease_typeid) {
        this.message_disease_typeid = message_disease_typeid;
    }

    public void setMessage_disease_type(String message_disease_type) {
        this.message_disease_type = message_disease_type;
    }

    public String getMessage_disease_typeid() {
        return message_disease_typeid;
    }

    public String getMessage_disease_type() {
        return message_disease_type;
    }
}

public static class MemMessageRecordssEntity {
    private String message_diseaseid;
    private String message_disease;

    public void setMessage_diseaseid(String message_diseaseid) {
        this.message_diseaseid = message_diseaseid;
    }

    public void setMessage_disease(String message_disease) {
        this.message_disease = message_disease;
    }

    public String getMessage_diseaseid() {
        return message_diseaseid;
    }

    public String getMessage_disease() {
        return message_disease;
    }
}

}

}

qq_33720124
qq_33720124   2016.02.22 10:50

图片说明

这是我自己目前解析得到的数据

rui888
rui888   Ds   Rxr 2016.02.22 11:02

具体看你的代码,你地下那些都是空。建议调试看看。

qq_33720124
qq_33720124 我自己得到的服务器返回也贴在下面了。
接近 2 年之前 回复
qq_33720124
qq_33720124 好像贴的有点乱。 =。= 第一次来CSDN。。不好意思
接近 2 年之前 回复
qq_33720124
qq_33720124 我把代码贴出来。
接近 2 年之前 回复
qq_33720124
qq_33720124   2016.02.22 11:06
  1. package sy;package sy;

import android.app.Activity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xiaheng.bswk.HttpInfo;
import com.xiaheng.bswk.HttpUtils;
import com.xiaheng.bswk.R;
import com.xiaheng.gsonBean.JiBingbean;
import com.xiaheng.gsonBean.JiBingdata;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**

  • Created by Administrator on 2016/2/18.
    */
    public class Getjzsdisease extends Activity {

    private TextView tv;
    private ListView jzsdisease_list;
    private LayoutInflater inflater;
    private TextView baocun;
    private static int currentPosition = -1;
    private JiBingbean jiBingbean;
    private Handler myHandler = null;
    private ArrayList jibingdata = new ArrayList<>();
    private JbAdapter adapter;
    private String date_time;
    private boolean dui1 = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_jzsdisease_list);
    myHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    if (101 == msg.what) {
    for (int i = 0; i < jiBingbean.getData().getMem_message_records().size(); i++) {
    JiBingdata data1 = new JiBingdata();

                    //获得数据
                    String message_disease = jiBingbean.getData().getMem_message_records().get(i).getMessage_disease();
                    System.out.println("message_disease" + message_disease);
                    String message_disease_type = jiBingbean.getData().getMem_message_records().get(i).getMessage_disease_type();
    
                    data1.setMessage_disease(message_disease);
                    data1.setMessage_disease_type(message_disease_type);
    
                    jibingdata.add(data1);
                }
            }
            adapter = new JbAdapter();
            jzsdisease_list.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    };
    new Thread(new Mythread()).start();
    setview();
    

    }
    private void setview() {
    //保存按钮
    baocun = (TextView) findViewById(R.id.textView71);
    baocun.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        }
    });
    //listView监听
    jzsdisease_list = (ListView) findViewById(R.id.mylist_disease);
    jzsdisease_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
            currentPosition = position;
            adapter.notifyDataSetChanged();
        }
    });
    

    }
    //解析JSON数据
    private class Mythread extends Thread {
    @Override
    public void run() {
    System.out.println("....启动线程");
    //网络请求路径
    String path = HttpInfo.PATH + HttpInfo.GET_JIBINGLIST + HttpInfo.ID;
    String jsonString = HttpUtils.getJsonContent(path);
    Gson gson = new Gson();
    System.out.println("------------------------"+path);
    //解析json数据
    jiBingbean = gson.fromJson(jsonString, JiBingbean.class);

        System.out.println("....启动线程" + jiBingbean);
    

    //解析成功后发送msg给UI主线程
    Message msg = new Message();
    msg.what = 101;
    myHandler.sendMessage(msg);
    }
    }
    //自定义adapter
    class JbAdapter extends BaseAdapter {

    public JbAdapter() {
        super();
        inflater = LayoutInflater.from(Getjzsdisease.this);
    }
    @Override
    public int getCount() {
        return jibingdata.size();
    }
    
    @Override
    public Object getItem(int position) {
        return position;
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder view = null;
        TextView date=null;
        if (convertView == null) {
            view = new ViewHolder();
            convertView = inflater.inflate(R.layout.get_jzsdisease, parent, false);
            view.jibing_type = (TextView) convertView
                    .findViewById(R.id.xinxueguanxitong_text);
    
            view.jibing_name = (TextView) convertView
                    .findViewById(R.id.jzs_jibingmingcheng);
            view.tang= (ImageView) convertView.findViewById(R.id.tang);
            view.relativeLayout= (RelativeLayout)
                    convertView.findViewById(R.id.addjiazushi_date1);
            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }
    

    //日期事件

        final ViewHolder finalView = view;
        final ViewHolder finalView1 = view;
        //点击图片是是否变色
        view.tang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!dui1) {
                    finalView.tang.setImageResource(R.drawable.dui_hover);
                    dui1 = true;
                } else {
                    finalView1.tang.setImageResource(R.drawable.dui);
                    dui1 = false;
                }
            }
        });
        //日期点击事件
        view.relativeLayout.setTag(position);
        view.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final TextView textView = (TextView) v.findViewById(R.id.jzs_adddate);//展示日期的textView
                final Calendar calendar = Calendar.getInstance();
                DatePickerDialog dialog = new DatePickerDialog(Getjzsdisease.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        calendar.set(year, monthOfYear, dayOfMonth);
                        System.out.println("-----------------date---------------" + DateFormat.format("yyyy-MM-dd", calendar));
                        textView.setText(DateFormat.format("yyyy-MM-dd", calendar));
                        date_time = textView.getText().toString();
                        System.out.println("------------------date--------------" + date_time);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                dialog.show();
            }
        });
        view.jibing_type.setText(jibingdata.get(position).getMessage_disease_type());
        view.jibing_name.setText(jibingdata.get(position).getMessage_disease());
        return convertView;
    }
    private class ViewHolder {
        private TextView jibing_type;
        private TextView jibing_name;
        private ImageView tang;
        private RelativeLayout relativeLayout;
    }
    

    }
    }

qq_33720124
qq_33720124   2016.02.22 11:06
  1. package sy;package sy;

import android.app.Activity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xiaheng.bswk.HttpInfo;
import com.xiaheng.bswk.HttpUtils;
import com.xiaheng.bswk.R;
import com.xiaheng.gsonBean.JiBingbean;
import com.xiaheng.gsonBean.JiBingdata;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**

  • Created by Administrator on 2016/2/18.
    */
    public class Getjzsdisease extends Activity {

    private TextView tv;
    private ListView jzsdisease_list;
    private LayoutInflater inflater;
    private TextView baocun;
    private static int currentPosition = -1;
    private JiBingbean jiBingbean;
    private Handler myHandler = null;
    private ArrayList jibingdata = new ArrayList<>();
    private JbAdapter adapter;
    private String date_time;
    private boolean dui1 = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_jzsdisease_list);
    myHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    if (101 == msg.what) {
    for (int i = 0; i < jiBingbean.getData().getMem_message_records().size(); i++) {
    JiBingdata data1 = new JiBingdata();

                    //获得数据
                    String message_disease = jiBingbean.getData().getMem_message_records().get(i).getMessage_disease();
                    System.out.println("message_disease" + message_disease);
                    String message_disease_type = jiBingbean.getData().getMem_message_records().get(i).getMessage_disease_type();
    
                    data1.setMessage_disease(message_disease);
                    data1.setMessage_disease_type(message_disease_type);
    
                    jibingdata.add(data1);
                }
            }
            adapter = new JbAdapter();
            jzsdisease_list.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    };
    new Thread(new Mythread()).start();
    setview();
    

    }
    private void setview() {
    //保存按钮
    baocun = (TextView) findViewById(R.id.textView71);
    baocun.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        }
    });
    //listView监听
    jzsdisease_list = (ListView) findViewById(R.id.mylist_disease);
    jzsdisease_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
            currentPosition = position;
            adapter.notifyDataSetChanged();
        }
    });
    

    }
    //解析JSON数据
    private class Mythread extends Thread {
    @Override
    public void run() {
    System.out.println("....启动线程");
    //网络请求路径
    String path = HttpInfo.PATH + HttpInfo.GET_JIBINGLIST + HttpInfo.ID;
    String jsonString = HttpUtils.getJsonContent(path);
    Gson gson = new Gson();
    System.out.println("------------------------"+path);
    //解析json数据
    jiBingbean = gson.fromJson(jsonString, JiBingbean.class);

        System.out.println("....启动线程" + jiBingbean);
    

    //解析成功后发送msg给UI主线程
    Message msg = new Message();
    msg.what = 101;
    myHandler.sendMessage(msg);
    }
    }
    //自定义adapter
    class JbAdapter extends BaseAdapter {

    public JbAdapter() {
        super();
        inflater = LayoutInflater.from(Getjzsdisease.this);
    }
    @Override
    public int getCount() {
        return jibingdata.size();
    }
    
    @Override
    public Object getItem(int position) {
        return position;
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder view = null;
        TextView date=null;
        if (convertView == null) {
            view = new ViewHolder();
            convertView = inflater.inflate(R.layout.get_jzsdisease, parent, false);
            view.jibing_type = (TextView) convertView
                    .findViewById(R.id.xinxueguanxitong_text);
    
            view.jibing_name = (TextView) convertView
                    .findViewById(R.id.jzs_jibingmingcheng);
            view.tang= (ImageView) convertView.findViewById(R.id.tang);
            view.relativeLayout= (RelativeLayout)
                    convertView.findViewById(R.id.addjiazushi_date1);
            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }
    

    //日期事件

        final ViewHolder finalView = view;
        final ViewHolder finalView1 = view;
        //点击图片是是否变色
        view.tang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!dui1) {
                    finalView.tang.setImageResource(R.drawable.dui_hover);
                    dui1 = true;
                } else {
                    finalView1.tang.setImageResource(R.drawable.dui);
                    dui1 = false;
                }
            }
        });
        //日期点击事件
        view.relativeLayout.setTag(position);
        view.relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final TextView textView = (TextView) v.findViewById(R.id.jzs_adddate);//展示日期的textView
                final Calendar calendar = Calendar.getInstance();
                DatePickerDialog dialog = new DatePickerDialog(Getjzsdisease.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        calendar.set(year, monthOfYear, dayOfMonth);
                        System.out.println("-----------------date---------------" + DateFormat.format("yyyy-MM-dd", calendar));
                        textView.setText(DateFormat.format("yyyy-MM-dd", calendar));
                        date_time = textView.getText().toString();
                        System.out.println("------------------date--------------" + date_time);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                dialog.show();
            }
        });
        view.jibing_type.setText(jibingdata.get(position).getMessage_disease_type());
        view.jibing_name.setText(jibingdata.get(position).getMessage_disease());
        return convertView;
    }
    private class ViewHolder {
        private TextView jibing_type;
        private TextView jibing_name;
        private ImageView tang;
        private RelativeLayout relativeLayout;
    }
    

    }
    }

qq_33720124
qq_33720124   2016.02.22 11:10

.启动线程JiBingbean{code='200', message='调用成功', data=JiBinglist{mem_uid='null', mem_message_records=[JiBingdata{message_diseaseid='null', message_disease_typeid='1', message_disease='null', message_disease_type='心血管系统'}, JiBingdata{message_diseaseid='null', message_disease_typeid='2', message_disease='null', message_disease_type='外围血管疾病'}, JiBingdata{message_diseaseid='null', message_disease_typeid='3', message_disease='null', message_disease_type='脑血管疾病'}, JiBingdata{message_diseaseid='null', message_disease_typeid='4', message_disease='null', message_disease_type='肺部疾病'}, JiBingdata{message_diseaseid='null', message_disease_typeid='5', message_disease='null', message_disease_type='代谢疾病'}, JiBingdata{message_diseaseid='null', message_disease_typeid='6', message_disease='null', message_disease_type='周围神经疾病'}, JiBingdata{message_diseaseid='null', message_disease_typeid='7', message_disease='null', message_disease_type='其他疾病'}]}}

这是我自己解析得到的返回值。

rui888
rui888   Ds   Rxr 2016.02.22 11:16

大哥,代码太多,建议还是debug调试看看

qq_33720124
qq_33720124 我也感觉 有点多 = = 我再调试一下把。
接近 2 年之前 回复
共13条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!