1.将从服务器获取到的数据封装成一个JSONObject对象
JSONObject json = new JSONObject(result);
2.通过对应的key可以获得到对应的values
String dataString = (String) json.get(dataString);
3.利用Gson解析将得到的dataString封装成对象
Gson gson = new Gson();--------》记得引入gson包
person p = gson.fromJson(dataString,person.class);
4.填充数据到界面
initView()----这里将p的属性的数据直接填入到界面中
代码如下:
定义的对象
================================================================================================================别人写的代码
JSONTokener jsonParser = new JSONTokener(commonData);
JSONObject json;
json = (JSONObject) jsonParser.nextValue();
sessionid = json.getString("sessionid");
JSONArray jsonArray = json.getJSONArray("dataList");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = (JSONObject) jsonArray.opt(i);
amt = jsonObject2.getString("amt");
System.out.println("amt的值:" + amt);
// 通过key去获取值
date = jsonObject2.getString("date");
details = jsonObject2.getString("details");
Constans.setComanyName(details);
bz = jsonObject2.getString("bz");
// Constans.setSuodingjine(bz);
receivingside = jsonObject2.getString("receivingside");
retcode = jsonObject2.getString("retcode");
retmsg = jsonObject2.getString("retmsg");
shoppingmall = jsonObject2.getString("shoppingmall");
state = jsonObject2.getString("state");
type = jsonObject2.getString("type");
}
} catch (Exception e) {
e.printStackTrace();
}
其实这段代码差不多就是gson解析的解析原理,自己以代码的方式去解析一个json的字符串