一、 使用原生方式解析
准备工作:准备一个布局文件,用来显示元数据与转换之后的数据
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_String2JOSNObject" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用原生方式:String2JOSNObject " /> <Button android:id="@+id/btn_String2JOSNArray" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用原生方式:String2JOSNArray " /> <Button android:id="@+id/btn_String2Bean_Gson" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用Gson方式:String2Bean " /> <Button android:id="@+id/btn_String2List_Gson" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用Gson方式:String2List " /> <Button android:id="@+id/btn_String2Arrays_Gson" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用Gson方式:String2Arrays " /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="元数据" android:textColor="@android:color/holo_red_light" android:textSize="20sp" /> <TextView android:id="@+id/tv_metadata" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="转换后的数据" android:textColor="@android:color/holo_red_light" android:textSize="20sp" /> <TextView android:id="@+id/tv_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> </LinearLayout>
1. 使用原生方式---将String转换为JOSNObject
1.1 准备元数据
//使用原生方式: JSONObject2String JSONObject jsonObject = new JSONObject(); try { jsonObject.put("name","小米"); jsonObject.put("age",1); JSONObject familyJOSN = new JSONObject(); familyJOSN.put("father","米爸"); familyJOSN.put("mother","米妈"); jsonObject.put("family",familyJOSN); JSONArray hobbyArray = new JSONArray(); hobbyArray.put("篮球"); hobbyArray.put("看书"); jsonObject.put("hobby",hobbyArray); } catch (JSONException e) { e.printStackTrace(); } metaDataStr = jsonObject.toString(); tv_metadata.setText(jsonObject.toString());
1.2 开始转换数据
//使用原生方式:String2JOSNObject findViewById(R.id.btn_String2JOSNObject).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { JSONObject jsonObject = new JSONObject(metaDataStr); Log.e(JOSNActivity.class.getName(),jsonObject.toString()); String name = jsonObject.getString("name"); String age = jsonObject.getString("age"); JSONObject familyJOSN = new JSONObject(String.valueOf(jsonObject.getJSONObject("family"))); String father = familyJOSN.getString("father"); String mother = familyJOSN.getString("mother"); JSONArray hobbyArray = new JSONArray(jsonObject.getJSONArray("hobby").toString()); String hobby = ""; for(int i = 0; i < hobbyArray.length(); i ++){ if(i != hobbyArray.length() -1){ hobby = hobby + (String) hobbyArray.get(i)+"、"; }else { hobby = hobby + (String) hobbyArray.get(i); } } String resultStr = "姓名:"+name+",年龄:"+age+",家庭成员:"+father+"、"+mother+",爱好:"+hobby; tv_data.setText(resultStr); } catch (JSONException e) { e.printStackTrace(); } } });
2. 使用原生方式:将String转换为JOSNArray
2.1 准备元数据
// JSONArray2String JSONArray array = new JSONArray(); for(int i = 0; i < 2; i ++){ JSONObject jsonObject = new JSONObject(); try { jsonObject.put("name","小米"+i); jsonObject.put("age",22+i); array.put(jsonObject); } catch (JSONException e) { e.printStackTrace(); } } metaDataStr = array.toString(); tv_metadata.setText(metaDataStr);
2.2 开始转换数据
//使用原生方式:String2JOSNArray findViewById(R.id.btn_String2JOSNArray).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { String resultStr = ""; JSONArray jsonArray = new JSONArray(metaDataStr); for(int i = 0; i < jsonArray.length(); i ++){ JSONObject jsonObject = (JSONObject) jsonArray.get(i); resultStr = resultStr + "第" + i + "组姓名:"+ jsonObject.getString("name") + ",年龄:"+ jsonObject.getString("age")+" "; } tv_data.setText(resultStr); } catch (JSONException e) { e.printStackTrace(); } } });
二、 使用GSON解析JSON数据
1. 使用Gson方式:将String转换为Bean
1.1 准备Bean类
static class Bean{ public Bean(String name,int age,List<String> hobby){ this.name = name; this.age = age; this.hobby = hobby; Family family = new Family(); family.setFather(name+"爸"); family.setMother(name+"妈"); this.family = family; } /** * name : 小米 * family : {"mother":"米妈","father":"米爸"} * age : 1 * hobby : ["篮球","看书"] */ private String name; private Family family; private int age; private List<String> hobby; public void setName(String name) { this.name = name; } public void setFamily(Family family) { this.family = family; } public void setAge(int age) { this.age = age; } public void setHobby(List<String> hobby) { this.hobby = hobby; } public String getName() { return name; } public Family getFamily() { return family; } public int getAge() { return age; } public List<String> getHobby() { return hobby; } public static class Family { /** * mother : 米妈 * father : 米爸 */ private String mother; private String father; public void setMother(String mother) { this.mother = mother; } public void setFather(String father) { this.father = father; } public String getMother() { return mother; } public String getFather() { return father; } } }
1.2 准备元数据
//使用Gson方式:Bean2String Gson gson = new Gson(); List<String> list = new ArrayList<>(); list.add("篮球"); list.add("看书"); Bean bean = new Bean("小米",23,list); metaDataStr = gson.toJson(bean).toString(); tv_metadata.setText(metaDataStr);
1.3 开始转换数据
//使用Gson方式:String2Bean findViewById(R.id.btn_String2Bean_Gson).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Gson gson = new Gson(); Bean resultBean = gson.fromJson(metaDataStr,Bean.class); String name = resultBean.getName(); int age = resultBean.getAge(); String father = resultBean.getFamily().getFather(); String mother = resultBean.getFamily().getMother(); List<String> list = resultBean.getHobby(); String hobby = ""; for(int i = 0; i < list.size(); i ++){ if(i != list.size() -1){ hobby = hobby + (String) list.get(i)+"、"; }else { hobby = hobby + (String) list.get(i); } } String resultStr = "姓名:"+name+",年龄:"+age+",家庭成员:"+father+"、"+mother+",爱好:"+hobby; tv_data.setText(resultStr); } });
2. 使用Gson方式:将String转换为List
2.1 准备元数据
//使用Gson方式:List2String Gson gson = new Gson(); List<Bean> beans = new ArrayList<>(); List<String> list = new ArrayList<>(); list.add("篮球"); list.add("看书"); Bean bean1 = new Bean("小米",23,list); Bean bean2 = new Bean("菲菲",24,list); beans.add(bean1); beans.add(bean2); metaDataStr = gson.toJson(beans); tv_metadata.setText(metaDataStr);
2.2 开始转换数据
//使用Gson方式:String2List findViewById(R.id.btn_String2List_Gson).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Gson gson = new Gson(); // Gson为我们提供了TypeToken来实现对泛型的支持, List<Bean> beans = gson.fromJson(metaDataStr,new TypeToken<List<Bean>>(){}.getType()); String resultStr =""; for(int i = 0; i < beans.size() ; i++){ String name = beans.get(i).getName(); int age = beans.get(i).getAge(); String father = beans.get(i).getFamily().getFather(); String mother = beans.get(i).getFamily().getMother(); resultStr = resultStr + "姓名:"+name+",年龄:"+age+",家庭成员:"+father+"、"+mother+" "; } tv_data.setText(resultStr); } });
3. 使用Gson方式:将String转换为Arrays
3.1 准备元数据
//使用Gson方式:Arrays2String String[] arrays = {"张三","李四","王五","赵六"}; Gson gson = new Gson(); metaDataStr = gson.toJson(arrays); tv_metadata.setText(metaDataStr);
3.2 开始转换数据
//使用Gson方式:String2Arrays findViewById(R.id.btn_String2Arrays_Gson).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Gson gson = new Gson(); //此时String[]当成一个实体类 String[] arrays = gson.fromJson(metaDataStr,String[].class); String resultStr = ""; for(int i = 0; i < arrays.length;i ++){ resultStr = resultStr + arrays[i] +";"; } tv_data.setText(resultStr); } });