packagecom.why.project.androidcnblogsdemo.activity;importandroid.content.Context;importandroid.os.Bundle;importandroid.support.v7.app.AppCompatActivity;importandroid.util.Log;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;importandroid.widget.Toast;importcom.why.project.androidcnblogsdemo.R;importcom.why.project.androidcnblogsdemo.model.ParseDataModel;importorg.json.JSONArray;importorg.json.JSONException;importorg.json.JSONObject;importjava.io.ByteArrayOutputStream;importjava.io.InputStream;importjava.util.ArrayList;/*** Created by HaiyuKing
* Used 解析data字段分别赋值Integer、String、JsonObject对象数据的JsonObject对象*/
public class ParseDataActivity extendsAppCompatActivity {private static final String TAG = "ParseDataActivity";privateButton mShowBtn;privateTextView mShowTv;private ArrayListjsonDataList;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parsedata);
initViews();
initData();
initEvents();
}private voidinitViews() {
mShowBtn=(Button) findViewById(R.id.btn_show);
mShowTv=(TextView) findViewById(R.id.tv_show);
}private voidinitData() {
jsonDataList= new ArrayList();//从Assets中获取json数据字符串
String jsonData = getStringFromAssert(this,"parseDataJson.json");
Log.e(TAG,"jsonData="+jsonData);//解析json格式的字符串
try{
JSONArray jsonDataArray= newJSONArray(jsonData);if(jsonDataArray.length() > 0){for(int i=0; i
JSONObject jsonDataItemObj=jsonDataArray.getJSONObject(i);
ParseDataModel model= newParseDataModel();
model.setCode(jsonDataItemObj.getInt("code"));
model.setData(jsonDataItemObj.get("data"));
jsonDataList.add(model);
}
}
}catch(JSONException e) {
e.printStackTrace();
}//方案二,通过Gson解析,不过只能解析出来String类型,其他类型均为Object//https://segmentfault.com/q/1010000002779358
//try {//JSONArray jsonDataArray = new JSONArray(jsonData);//if(jsonDataArray.length() > 0){//for(int i=0; i
//} catch (JSONException e) {//e.printStackTrace();//}//http://www.cnblogs.com/zhujiabin/p/4660828.html
//Type listType = new TypeToken>(){}.getType();//Gson gson = new Gson();//LinkedList parseDataModelsList = gson.fromJson(jsonData, listType);//
//String showResult = "";//
//for (Iterator iterator = parseDataModelsList.iterator(); iterator.hasNext();) {//ParseDataModel model = (ParseDataModel) iterator.next();//int code = model.getCode();//Object data = model.getData();//String dataType = "Object";//if(data instanceof Integer){//data = (Integer)data;//dataType = "Integer";//}else if(data instanceof String){//data = (String)data;//dataType = "String";//}else if(data instanceof JSONObject){//data = (JSONObject)data;//dataType = "JSONObject";//}//showResult += "code=" + code + ";data=" + data + "【dataType=" + dataType + "】\n";//}//mShowTv.setText(showResult);
}private voidinitEvents() {
mShowBtn.setOnClickListener(newView.OnClickListener() {
@Overridepublic voidonClick(View v) {
String showResult= "";//循环获取json数据
for(int i = 0;i
ParseDataModel model=jsonDataList.get(i);int code =model.getCode();
Object data=model.getData();
String dataType= "Object";if(data instanceofInteger){
data=(Integer)data;
dataType= "Integer";
}else if(data instanceofString){
data=(String)data;
dataType= "String";
}else if(data instanceofJSONObject){
data=(JSONObject)data;
dataType= "JSONObject";
}
showResult+= "code=" + code + ";data=" + data + "【dataType=" + dataType + "】\n";
}
mShowTv.setText(showResult);
}
});
}/*** 访问assets目录下的资源文件,获取文件中的字符串
*@paramassetsFilePath - 文件的相对路径,例如:"listitemdata.txt或者"/why/listdata.txt"
*@return内容字符串
**/
public staticString getStringFromAssert(Context mContext, String assetsFilePath) {
String content= ""; //结果字符串
try{
InputStream is= mContext.getResources().getAssets().open(assetsFilePath);//打开文件
int ch = 0;
ByteArrayOutputStream out= new ByteArrayOutputStream(); //实现了一个输出流
while ((ch = is.read()) != -1) {
out.write(ch);//将指定的字节写入此 byte 数组输出流
}byte[] buff = out.toByteArray();//以 byte 数组的形式返回此输出流的当前内容
out.close(); //关闭流
is.close(); //关闭流
content = new String(buff, "UTF-8"); //设置字符串编码
} catch(Exception e) {
Toast.makeText(mContext,"对不起,没有找到指定文件!", Toast.LENGTH_SHORT)
.show();
}returncontent;
}
}