android studio app 前端获取json数据以对象的形式来解析展示
2017-11-20 17:58
671 查看
一般正常情况下使用http网络请求得到的后台数据为json格式,例如:得到json数据为:data = {"id":"11","title":"标题","name":"小明"}。
//先写好对象bean文件
public class DataBean{
public String id;
public String title;
public String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//获取得到json数据,并进行json解析处理
JSONObject object = new JSONObject(data);
Gson gson = new Gson();
new DataBean() = gson.fromJson(String.valueOf(object),DataBean.class);
adapter.update(obdDataBean);//刷新数据
//如果是单个数据就直接进行赋值,如果是gridview数据,需要借助adapter进行处理
public class MyObdAdapter2 extends BaseAdapter {
DecimalFormat df = new DecimalFormat("#.00");
private Context context;
private ObdDataBean obdDataBean;
public MyObdAdapter2(Context context, ObdDataBean obdDataBean) {
this.context = context;
this.obdDataBean = obdDataBean;
}
@Override
public int getCount() {
return 16;//声明自己定死了只能是16条数据
}
public void update(ObdDataBean obdDataBean) {
this.obdDataBean = null;
this.obdDataBean = obdDataBean;
this.notifyDataSetChanged();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViiewHolder holder = null;
if (convertView == null) {
holder = new MyViiewHolder();
//把vlist layout转换成View【LayoutInflater的作用】
holder.id = (TextView) convertView.findViewById(R.id.id);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.name = (TextView) convertView.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (MyViiewHolder) convertView.getTag();
}
holder.id.setText(obdDataBean.getId() + "");
holder.title.setText(obdDataBean.getTitle() + "");
holder.name.setText(obdDataBean.getName() + "");
return convertView;
}
class MyViiewHolder {
private TextView id
private TextView title
private TextView name
}
}
//先写好对象bean文件
public class DataBean{
public String id;
public String title;
public String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//获取得到json数据,并进行json解析处理
JSONObject object = new JSONObject(data);
Gson gson = new Gson();
new DataBean() = gson.fromJson(String.valueOf(object),DataBean.class);
adapter.update(obdDataBean);//刷新数据
//如果是单个数据就直接进行赋值,如果是gridview数据,需要借助adapter进行处理
public class MyObdAdapter2 extends BaseAdapter {
DecimalFormat df = new DecimalFormat("#.00");
private Context context;
private ObdDataBean obdDataBean;
public MyObdAdapter2(Context context, ObdDataBean obdDataBean) {
this.context = context;
this.obdDataBean = obdDataBean;
}
@Override
public int getCount() {
return 16;//声明自己定死了只能是16条数据
}
public void update(ObdDataBean obdDataBean) {
this.obdDataBean = null;
this.obdDataBean = obdDataBean;
this.notifyDataSetChanged();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViiewHolder holder = null;
if (convertView == null) {
holder = new MyViiewHolder();
//把vlist layout转换成View【LayoutInflater的作用】
holder.id = (TextView) convertView.findViewById(R.id.id);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.name = (TextView) convertView.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (MyViiewHolder) convertView.getTag();
}
holder.id.setText(obdDataBean.getId() + "");
holder.title.setText(obdDataBean.getTitle() + "");
holder.name.setText(obdDataBean.getName() + "");
return convertView;
}
class MyViiewHolder {
private TextView id
private TextView title
private TextView name
}
}
相关文章推荐
- 前台js获取到后台json数据,eval解析json后,日期(object)对象转换为date日期格式显示
- MVC 中前端传递Json数据,后端以对象形式接受参数的处理方法
- 跨域获取JSON数据形式的对象(含属性与方法)
- 本文以实例形式展示了php解析json数据的方法
- js 将json字符串转换为json对象的方法解析 作者: 字体:[增加 减小] 类型:转载 将json字符串转换为json对象的方法。在数据传输过程中,json是以文本,即字符串的形式传递的,而J
- 前端 ajax 获取后台json数据 解析
- struts2 + ajax(从后台获取json格式的数据返回到前端,然后前端用jquery对json数据进行解析)
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及文件上传的部分
- iOS--JSON解析后如何获取数据,并且展示到相应cell上
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及非文件上传的部分
- ajax解析json对象数据到前端
- 解析JSON对象与字符串之间的相互转换(获取json数据转换为字符串,json传值方法)
- httpResponse的getEntity为空,获取了这个数据在解析的时候出现了String不能转换成json对象
- 前端将json数据解析成js对象失败
- 跨平台获取外部系统的数据解析成json并传输到前台展示成列表
- java代码解析json对象获取json对象属性值
- Android客户端从服务器端获取json数据并解析的实现代码
- android客户端从服务器端获取json数据并解析的实现代码(重要)
- GSON解析json含有不确定的泛型实体类,根据键值获取json数据
- HttpClient获取并解析JSON数据