标签:byte obj mode aar tty 步骤 height div line
当解析Json数据时,如果碰到同字段不同数据类型的情况,如何处理呢?比如:
[
{
"code": 1,
"data": 33
},
{
"code": 2,
"data": "string"
},
{
"code": 3,
"data": {
"age": 22,
"name": "rc"
}
}
]
解析思路:将data声明为Object类型,然后取值的时候,通过instanceof判断属于哪种类型。
package com.why.project.androidcnblogsdemo.model; /** * Created by HaiyuKing * Used 解析data字段分别赋值Integer、String、JsonObject对象数据的JsonObject对象 */ public class ParseDataModel { private int code; private Object data; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="显示JSONObject数据" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_show" android:layout_gravity="center_horizontal"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_show" android:hint="此处显示JSONObject数据"/> </LinearLayout>
package com.why.project.androidcnblogsdemo.activity; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.why.project.androidcnblogsdemo.R; import com.why.project.androidcnblogsdemo.model.ParseDataModel; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.ArrayList; /** * Created by HaiyuKing * Used 解析data字段分别赋值Integer、String、JsonObject对象数据的JsonObject对象 */ public class ParseDataActivity extends AppCompatActivity { private static final String TAG = "ParseDataActivity"; private Button mShowBtn; private TextView mShowTv; private ArrayList<ParseDataModel> jsonDataList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_parsedata); initViews(); initData(); initEvents(); } private void initViews() { mShowBtn = (Button) findViewById(R.id.btn_show); mShowTv = (TextView) findViewById(R.id.tv_show); } private void initData() { jsonDataList = new ArrayList<ParseDataModel>(); //从Assets中获取json数据字符串 String jsonData = getStringFromAssert(this,"parseDataJson.json"); Log.e(TAG,"jsonData="+jsonData); //解析json格式的字符串 try { JSONArray jsonDataArray = new JSONArray(jsonData); if(jsonDataArray.length() > 0){ for(int i=0; i<jsonDataArray.length();i++){ JSONObject jsonDataItemObj = jsonDataArray.getJSONObject(i); ParseDataModel model = new ParseDataModel(); 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<jsonDataArray.length();i++){ // jsonDataList.add(new Gson().fromJson(jsonDataArray.getString(i),ParseDataModel.class)); // } // } // // } catch (JSONException e) { // e.printStackTrace(); // } //http://www.cnblogs.com/zhujiabin/p/4660828.html // Type listType = new TypeToken<LinkedList<ParseDataModel>>(){}.getType(); // Gson gson = new Gson(); // LinkedList<ParseDataModel> 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 void initEvents() { mShowBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String showResult = ""; //循环获取json数据 for(int i = 0;i<jsonDataList.size();i++){ ParseDataModel model = jsonDataList.get(i); 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); } }); } /** * 访问assets目录下的资源文件,获取文件中的字符串 * @param assetsFilePath - 文件的相对路径,例如:"listitemdata.txt或者"/why/listdata.txt" * @return 内容字符串 * */ public static String 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(); } return content; } }
标签:byte obj mode aar tty 步骤 height div line
原文地址:http://www.cnblogs.com/whycxb/p/7789196.html