JSON

Android json解析有关内容学习笔记(2)

字号+ 作者:H5之家 来源:H5之家 2015-09-20 18:11 我要评论( )

之后便可以在activity中进行解析的操作了: ...............String result = null;try {result = HttpUtil.getRequest(:8080/jsontest/json.jsp);Log.i(PDA, result-------- + result);} catch (Exception e) {e.pr


之后便可以在activity中进行解析的操作了:

............... String result = null; try { result = HttpUtil.getRequest(":8080/jsontest/json.jsp"); Log.i("PDA", "result-------->" + result); } catch (Exception e) {e.printStackTrace();} JSONObject jsonObject = new JSONObject(result); txt_Name = (TextView) findViewById(R.id.txt_Name); txt_Name.setText(jsonObject.getString("Name")); txt_Race = (TextView) findViewById(R.id.txt_Race); txt_Race.setText(jsonObject.getString("Race")); txt_Profession = (TextView) findViewById(R.id.txt_Profession); txt_Profession.setText(jsonObject.getString("Profession")); txt_ServerMsg = (TextView) findViewById(R.id.txt_ServerMsg); txt_ServerMsg.setText(jsonObject.getJSONObject("Server").getString("Server_region")+","+jsonObject.getJSONObject("Server").getString("Server_name")); txt_Talent = (TextView) findViewById(R.id.txt_Talent); txt_Talent.setText(jsonObject.getJSONArray("Talent").getString(0)+","+jsonObject.getJSONArray("Talent").getString(1)); ............................


---------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 另外一种是直接构建json对象:

//创建一个json对象 jsonObject = new JSONObject(); //字符串值放入jsonObject中 jsonObject.put("Name", "辛德瑞拉点点"); jsonObject.put("Race", "人类"); jsonObject.put("Profession", "死亡骑士"); //服务器的信息的值是对象,故创建一个对象,然后添加到jsonObject对象中 JSONObject serverMsg = new JSONObject(); serverMsg.put("Server_region", "二区"); serverMsg.put("Server_name", "阿古斯"); jsonObject.put("Server", serverMsg); //天赋信息的值是数组,故创建一个数组,然后添加到jsonObject对象中 JSONArray talent = new JSONArray(); talent.put("鲜血").put("冰霜"); jsonObject.put("Talent", talent); //整数值放入jsonObject中 jsonObject.put("Achievement_Point", 12090); ...............................

之后的解析方式是一样的。

以下是解析之后的效果图:

 

                         

                                

虽然现在不常会用到Json解析,但这种数据交换方式一定要掌握才好~!


 

 

 

 

 

 

   


原文链接:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • php CI 实战教程:[5]用curl获取json并解析

    php CI 实战教程:[5]用curl获取json并解析

    2016-02-26 17:00

  •  JSON入门级学习总结-JSON数据结构

    JSON入门级学习总结-JSON数据结构

    2016-02-25 11:05

  • Android解析Json速度最快的库:json

    Android解析Json速度最快的库:json

    2016-02-13 18:00

  • JavaScript转换与解析JSON方法实例详解第1/2页

    JavaScript转换与解析JSON方法实例详解第1/2页

    2016-02-10 21:25

网友点评
0