1 package com.example.android_json; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 import java.io.UnsupportedEncodingException; 8 9 import org.json.JSONArray; 10 import org.json.JSONException; 11 12 import android.app.Activity; 13 import android.os.Bundle; 14 import android.widget.ListView; 15 16 public class MainActivity extends Activity { 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 ListView listView = (ListView) findViewById(R.id.listview); 23 24 InputStream inputStream = getResources().openRawResource(R.raw.person); 25 BufferedReader bufferedReader; 26 JSONArray jsonArray = null; 27 try { 28 bufferedReader = new BufferedReader(new InputStreamReader( 29 inputStream)); 30 StringBuffer stringBuffer = new StringBuffer(); 31 String line; 32 while ((line = bufferedReader.readLine()) != null) { 33 stringBuffer.append(line); 34 } 35 // 实列化JSONArray对象 36 jsonArray = new JSONArray(stringBuffer.toString()); 37 38 } catch (UnsupportedEncodingException e1) { 39 e1.printStackTrace(); 40 } catch (IOException e) { 41 42 e.printStackTrace(); 43 } catch (JSONException e) { 44 e.printStackTrace(); 45 } 46 listView.setAdapter(new ListViewAdapter(getApplicationContext(), 47 jsonArray)); 48 } 49 50 }
1 package com.example.android_json; 2 3 import org.json.JSONArray; 4 import org.json.JSONException; 5 import org.json.JSONObject; 6 7 import android.content.Context; 8 import android.view.LayoutInflater; 9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.widget.BaseAdapter; 12 import android.widget.TextView; 13 14 public class ListViewAdapter extends BaseAdapter { 15 LayoutInflater inflater; 16 JSONArray jsonArray; 17 18 public ListViewAdapter(Context context, JSONArray jsonArray) { 19 this.jsonArray = jsonArray; 20 inflater = LayoutInflater.from(context); 21 } 22 23 @Override 24 public int getCount() { 25 return jsonArray.length(); 26 } 27 28 @Override 29 public Object getItem(int position) { 30 try { 31 return jsonArray.get(position); 32 } catch (JSONException e) { 33 e.printStackTrace(); 34 } 35 return null; 36 } 37 38 @Override 39 public long getItemId(int position) { 40 return position; 41 } 42 43 @Override 44 public View getView(int position, View convertView, ViewGroup parent) { 45 View v = inflater.inflate(R.layout.listview_item, null); 46 TextView idTextView = (TextView) v.findViewById(R.id.item_id); 47 TextView nameTextView = (TextView) v.findViewById(R.id.item_name); 48 TextView sexTextView = (TextView) v.findViewById(R.id.item_sex); 49 50 try { 51 // 得到JSONObject 52 JSONObject jsonObject = jsonArray.getJSONObject(position); 53 // 得到映射的数据 54 int id = jsonObject.getInt("id"); 55 String name = jsonObject.getString("name"); 56 String sex = jsonObject.getString("sex"); 57 // 赋值 58 idTextView.setText(id + ""); 59 nameTextView.setText(name); 60 sexTextView.setText(sex); 61 } catch (JSONException e) { 62 e.printStackTrace(); 63 } 64 return v; 65 } 66 67 }
1 [{"id":1,"name":"xiaosan","sex":"男"},{"id":2,"name":"xiaowu","sex":"女"}]