Android解析JSON(原生态 Gson FastJson)
2017-03-05 10:08
459 查看
通过解析Json的三种方式来实现在Android客户端和服务器端使用json这种数据格式来进行数据的交换。
开始解析:从服务器访问过来的数据json解析到listview显示
Android客户端通过一个AsyncTask异步任务请求服务器端的某些数据,AsyncTask两个方法(1)获取网络数据:doInBackground();(2)更新UI(数据):onPostExecute()。在解析完这些数据后,onPostExecute()方法会将解析的数据内容更新到适配器,通知适配器发送改变。
原生态:纯的,jdbc底层代码,很繁琐
Gson(google旗下)解析:需要去下载Gson这个jar包,导入到我们的项目中。用Gson,我们可以非常轻松的实现数据对象和json对象的相互转化,其中我们最常用的方法fromJSON(),将json对象转换成我们需要的数据对象。
FastJson(阿里巴巴旗下)解析:需要导入第三方包,用法和Gson一样,实现数据对象和json对象的相互转化Gson是用fromJSON()方法,而FastJson用JSON.parseObject()方法,然后将json对象中的集合放入我们需要的数据对象集合中。
JSONObject:对象
JSONArray:集合
开始解析:从服务器访问过来的数据json解析到listview显示
public class GetJsonActivity extends AppCompatActivity { private ListView lv_main_getdata_json; private ProgressDialog progressDialog; private ListViewAdapter listViewAdapter; List<Userentity> listuser = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_get_json); lv_main_getdata_json = (ListView) findViewById(R.id.lv_main_getdata_json); listViewAdapter = new ListViewAdapter(); lv_main_getdata_json.setAdapter(listViewAdapter); progressDialog = new ProgressDialog(this); progressDialog.setMessage("正在拼命loading中..."); } class MyTask extends AsyncTask { @Override protected void onPreExecute() { super.onPreExecute(); progressDialog.show(); } @Override protected Object doInBackground(Object[] params) { final List<Userentity> Lists = new ArrayList<>(); //获取网络数据 //01.定义获取网络数据的路径 String path = getString(R.string.server_name) + "listuserjson.xhtml"; try { //02.实例化URL URL url = new URL(path); //03.获取对象链接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //04.设置请求方式 connection.setRequestMethod("GET"); //05.设置链接超时的时间 connection.setConnectTimeout(5000); //06.获取响应码 if (connection.getResponseCode() == 200) { //07.获取返回过来的数据(xml) InputStream is = connection.getInputStream(); //08.测试(删除--注释) //缓冲字符流 String str = null; BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuffer stringBuffer = new StringBuffer(); while ((str = br.readLine()) != null) { stringBuffer.append(str); } //09.JSON解析 //09.1原生态,纯的 // try { // JSONObject jsonObject = new JSONObject(stringBuffer.toString()); // String clazz = jsonObject.getString("clazz"); // int number = jsonObject.getInt("number"); // JSONArray jsonArray = jsonObject.getJSONArray("userall"); // for (int i = 0; i < jsonArray.length(); i++) { // JSONObject object = jsonArray.getJSONObject(i); // String uid = object.getString("uid"); // String uname = object.getString("uname"); // String upwd = object.getString("upwd"); // Userentity user=new Userentity(uid,uname,upwd); // listuser.add(user); // } // } catch (JSONException e) { // e.printStackTrace(); // } // //09.2gson // Gson gson=new Gson(); // BigUser biguser=gson.fromJson(stringBuffer.toString(),BigUser.class); // String clazz=biguser.getClazz(); // int number=biguser.getNumber(); // listuser.addAll(biguser.getUserall()); //09.3Fastjson BigUser biguser=JSON.parseObject(stringBuffer.toString(),BigUser.class); String clazz=biguser.getClazz(); int number=biguser.getNumber(); listuser.addAll(biguser.getUserall()); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Object object) { super.onPostExecute(object); listViewAdapter.notifyDataSetChanged(); progressDialog.cancel(); } } public void getDataJSON(View view) { new MyTask().execute(); } //给listview设置适配器 class ListViewAdapter extends BaseAdapter { @Override public int getCount() { return listuser.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = getLayoutInflater().inflate(R.layout.activity_textview, null); //拿控件 TextView list_text_id = (TextView) view.findViewById(R.id.list_text_id); TextView list_text_name = (TextView) view.findViewById(R.id.list_text_name); TextView list_text_pwd = (TextView) view.findViewById(R.id.list_text_pwd); //绑定数值 list_text_id.setText(listuser.get(position).getUid()); list_text_name.setText(listuser.get(position).getUname()); list_text_pwd.setText(listuser.get(position).getUpwd()); return view; } } }
Android客户端通过一个AsyncTask异步任务请求服务器端的某些数据,AsyncTask两个方法(1)获取网络数据:doInBackground();(2)更新UI(数据):onPostExecute()。在解析完这些数据后,onPostExecute()方法会将解析的数据内容更新到适配器,通知适配器发送改变。
原生态:纯的,jdbc底层代码,很繁琐
Gson(google旗下)解析:需要去下载Gson这个jar包,导入到我们的项目中。用Gson,我们可以非常轻松的实现数据对象和json对象的相互转化,其中我们最常用的方法fromJSON(),将json对象转换成我们需要的数据对象。
FastJson(阿里巴巴旗下)解析:需要导入第三方包,用法和Gson一样,实现数据对象和json对象的相互转化Gson是用fromJSON()方法,而FastJson用JSON.parseObject()方法,然后将json对象中的集合放入我们需要的数据对象集合中。
JSONObject:对象
JSONArray:集合
相关文章推荐
- Android之解析JSON数据(android原生态,FastJson,Gson)
- Android之解析JSON数据示例(android原生态,FastJson,Gson)
- Android常用Json解析框架GSON和FastJson的使用
- Android Jackson、Gson、FastJson解析框架对比
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
- JSON解析-GSON-FastJSON.android
- Android学习(二):解析JSON,GSON,FASTJSON;JDBC编程
- Android进阶笔记14:3种JSON解析工具(org.json、fastjson、gson)
- Android JSON 解析库的使用 - Gson 和 fast-json
- Android-解析Json(Gson-FastJson)
- Android实战——GsonFormat、Gson、fastjson快速搞定JSON数据解析
- Android中的JSON解析方式:json; Gson ; Fastjson
- Android中Json数据解析(二)--使用Gson、Jackson和FastJson解析Json数据
- android Json Gson FastJson 解析
- android json解析(fastjson,Gson等)
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
- Android之json解析(FastJson Gson 对比)
- Android中Json数据解析(二)--使用Gson、Jackson和FastJson解析Json数据
- 原生态,Gson,FastJson解析Json