Android Json数据的解析 java数据解析
0.xml布局
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="cn.ldc456sina.myjson.MainActivity"> 11 12 <ScrollView 13 android:layout_width="match_parent" 14 android:layout_height="match_parent"> 15 16 <LinearLayout 17 android:orientation="vertical" 18 android:layout_width="match_parent" 19 android:layout_height="match_parent"> 20 21 <EditText 22 android:id="@+id/show" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:background="#000000" 26 android:enabled="false" 27 android:gravity="center" 28 android:hint="@string/ldc_str" 29 android:textColor="#990000" /> 30 31 <Button 32 android:id="@+id/btn1" 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content" 35 android:layout_below="@id/show" 36 android:layout_margin="3dp" 37 android:text="json数据解析" 38 android:textSize="20sp" 39 android:textStyle="bold" /> 40 41 <Button 42 android:id="@+id/btn2" 43 android:layout_width="match_parent" 44 android:layout_height="wrap_content" 45 android:layout_below="@id/btn1" 46 android:layout_margin="3dp" 47 android:text="json数组解析" 48 android:textSize="20sp" 49 android:textStyle="bold" /> 50 51 <Button 52 android:id="@+id/btn3" 53 android:layout_width="match_parent" 54 android:layout_height="wrap_content" 55 android:layout_below="@id/btn2" 56 android:layout_margin="3dp" 57 android:text="json数组数据解析" 58 android:textSize="20sp" 59 android:textStyle="bold" /> 60 </LinearLayout> 61 </ScrollView> 62 63 64 </RelativeLayout>
1.布局

2.
3.
4.
5json数据解析
/**
* 简单的json数据
*
* @return
* @throws JSONException
*/
1 public String createJson1() throws JSONException { 2 String result = ""; 3 JSONObject create_js = new JSONObject(); 4 create_js.put("name", "DigitalKing"); 5 create_js.put("age", 14); 6 create_js.put("address", "中国").put("province", "武汉"); 7 result = create_js.toString(); 8 return result; 9 }
/**
* json数组解析
* 创建一个学生数组
*
* @return
*/
1 public String createjson2() throws JSONException { 2 String result = ""; 3 JSONObject create_json2 = new JSONObject(); 4 JSONArray jsonArray = new JSONArray(); 5 jsonArray.put(0, "数字帝国"); 6 jsonArray.put(1, "23"); 7 jsonArray.put(2, "中国湖北省武汉市"); 8 jsonArray.put(3, "热干面"); 9 jsonArray.put(4, "樱花"); 10 create_json2.put("Student", jsonArray); 11 result = create_json2.toString(); 12 return result; 13 }
/**
* 数组数据解析
* 数组数据混合解析
*
* @return
*/
1 public String createjson3() throws JSONException { 2 String result = ""; 3 JSONObject create_json3 = new JSONObject(); 4 create_json3.put("name", "@小小古怪"); 5 create_json3.put("age", 222); 6 create_json3.put("school", "世界一流/家里蹲航天理工大学"); 7 JSONArray address = new JSONArray(); 8 address.put("中国").put("湖北省").put("武汉市").put("江夏区"); 9 create_json3.put("address", address); 10 result = create_json3.toString(); 11 return result; 12 }
/**
* 解析简单的json数据
*/
1 public void fun1() { 2 //解析数据 3 try { 4 String resule_str = createJson1(); 5 JSONObject js = new JSONObject(resule_str); 6 String name = js.getString("name"); 7 int age = js.getInt("age"); 8 String address = js.getString("address"); 9 String province = js.getString("province"); 10 String result = resule_str + "\n\n\n ------------------------------\n 姓 名:" + name + " 年 龄:" + age + "\n 地 址:" + address + " " + province + "\n------------------------------\n"; 11 show.setText(result); 12 } catch (JSONException e) { 13 e.printStackTrace(); 14 } 15 }
/**
* 解析json数组数据
*/
1 public void fun2() { 2 try { 3 String result = createjson2(); 4 String vals = ""; 5 JSONObject jsonObject = new JSONObject(result); 6 JSONArray ja = jsonObject.getJSONArray("Student"); 7 int length = ja.length(); 8 for (int i = 0; i < length; i++) { 9 vals += ja.getString(i) + "|\n"; 10 } 11 show.setText(result + "\n\n ------------------------\n" + vals + "\n----------------------"); 12 } catch (JSONException ex_str) { 13 ex_str.printStackTrace(); 14 } 15 }
//json数组数据解析
1 public void fun3() { 2 try { 3 String result = createjson3(); 4 String val_str = "";//获取数组的值 5 JSONObject jsonObject = new JSONObject(result); 6 String nameval = jsonObject.getString("name"); 7 int ageval = jsonObject.getInt("age"); 8 String schoolval = jsonObject.getString("school"); 9 JSONArray ja = jsonObject.getJSONArray("address"); 10 int length = ja.length(); 11 for (int i = 0; i < length; i++) { 12 val_str += ja.get(i); 13 } 14 show.setText(result+"\n\n\n-----------------------------------\n" + "姓名 :" + nameval + " 年龄:" + ageval + "\n 学校:" + schoolval + "\n" + val_str + "\n--------------------------------------\n"); 15 } catch (JSONException ex_str) { 16 ex_str.printStackTrace(); 17 } 18 19 }
1 fun1();//简答的json数据解析 2 fun2();//json数组解析 3 fun3();//json数据数组解析 4 设置监听事件 5 show = (TextView) findViewById(R.id.show); 6 btn1 = (Button) findViewById(R.id.btn1); 7 btn2 = (Button) findViewById(R.id.btn2); 8 btn3 = (Button) findViewById(R.id.btn3); 9 btn1.setOnClickListener(new LDC_()); 10 btn2.setOnClickListener(new LDC_()); 11 btn3.setOnClickListener(new LDC_()); 12 13 在点击监听处理数 14 class LDC_ implements View.OnClickListener { 15 16 @Override 17 public void onClick(View v) { 18 switch (v.getId()) { 19 case R.id.btn1: 20 fun1();//简单的json解析 21 break; 22 case R.id.btn2: 23 fun2();//简单的json数组解析 24 break; 25 case R.id.btn3: 26 fun3();//数组数据解析 27 break; 28 } 29 30 31 } 32 }
//结束
@小小古怪
数字帝国
DigitalKing

浙公网安备 33010602011771号