Json解析字符串j简单实例
Json解析字符串实例,解析内容为:
{"info":[{"code":"C","key":"028","nearest":"NO","value":"???"},{"code":"N","key":"0771","nearest":"NO","value":"????"},{"code":"L","key":"0772","nearest":"NO","value":"????"},{"code":"G","key":"0773","nearest":"NO","value":"????"}],"resultCode":"0","resultInfo":"SUCCESS"}
网址为:http://www.bejson.com/go.html?u=http://www.bejson.com/jsonview2/(Json视图)
第一步:看JsonS视图,从最里层想外层写
这里强调一点,就是必须要一一对应才能解析
第一层代码:
package com.example.com.scxh.jsonjiexi;
//取名为CityBean
public class CityBean {
private String code;
private String key;
private String nearest;
private String value;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getNearest() {
return nearest;
}
public void setNearest(String nearest) {
this.nearest = nearest;
}
public String getLvalue() {
return value;
}
public void setLvalue(String lvalue) {
this.value = lvalue;
}
}
第二层:从里面往外数的第二层
package com.example.com.scxh.jsonjiexi;
import java.io.Serializable;
import java.util.List;
import junit.runner.Version;
import org.apache.http.entity.SerializableEntity;
public class UpdateDao implements Serializable{
private List<CityBean> info;
private String resultInfo;
private String resultCode;
public List<CityBean> getInfo() {
return info;
}
public void setInfo(List<CityBean> info) {
this.info = info;
}
public String getResultInfo() {
return resultInfo;
}
public void setResultInfo(String resultInfo) {
this.resultInfo = resultInfo;
}
public String getResultCode() {
return resultCode;
}
public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}
}
第三步:在主Activty里进行解析(这里另外我把适配器一起写在了主Activty里)
package com.example.com.scxh.jsonjiexi;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView mlListView;
private MyBaseAdapter mmAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlListView = (ListView) findViewById(R.id.listviews);
//读取文件夹assets的内容(本地内容)
String json = ReadAssetsFile.readtxt(this, "citymessage.txt");
Log.e("json", json);
// UpdateDao user = JSON.parseObject(json, UpdateDao.class);
Gson gson=new Gson();
UpdateDao user=gson.fromJson(json, UpdateDao.class);
List<CityBean> list = user.getInfo();
mmAdapter = new MyBaseAdapter(this);
mlListView.setAdapter(mmAdapter);
mmAdapter.setData(list);
}
public class MyBaseAdapter extends BaseAdapter {
private List<CityBean> mList = new ArrayList<CityBean>();
private LayoutInflater mInflater;
private Context mcontext;
public MyBaseAdapter(Context context) {
mcontext = context;
mInflater = LayoutInflater.from(mcontext);
}
public MyBaseAdapter(Context context, List<CityBean> list) {
mcontext = context;
mInflater = LayoutInflater.from(mcontext);
mList = list;
}
public void setData(List<CityBean> list) {
mList = list;
notifyDataSetChanged();
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.listview, null);
holder.titleTxt = (TextView) convertView
.findViewById(R.id.textviews);
holder.infoTxt = (TextView) convertView
.findViewById(R.id.textviewss);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
CityBean msg = (CityBean) getItem(position);
;
holder.titleTxt.setText(msg.getCode());
holder.infoTxt.setText(msg.getKey());
return convertView;
}
class ViewHolder {
TextView titleTxt = null;
TextView infoTxt = null;
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
这里把文件读取(流读取)单独封装承德一个类
package com.example.com.scxh.jsonjiexi;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
//封装类用于里的读取
public class ReadAssetsFile {
private static String text;
public static String readtxt(Context context, String name) {
try {
InputStream is = context.getAssets().open(name);
int size = is.available();
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
text = new String(buffer, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
return text;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。