之后便可以在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解析,但这种数据交换方式一定要掌握才好~!
原文链接: