用JSONObject,JSONObjectArray,JSONTokener,最后一个暂时没怎么用过

前端之家收集整理的这篇文章主要介绍了用JSONObject,JSONObjectArray,JSONTokener,最后一个暂时没怎么用过前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
String str = "http://www.lanjianlong.mobi/json/myselfinfo.php?userid=123456&secondVarle=?"

StringBuffer sbd = new StringBuffer();
try {
url = new URL(str);
HttpURLConnection url_con = (HttpURLConnection)url.openConnection();
url_con.setConnectTimeout(10*1000);
url_con.setRequestMethod("POST");
url_con.setDoOutput(true);
url_con.setUseCaches(false);
url_con.setRequestProperty("Content-type","application/x-java-serialized-object");
InputStream in =url_con.getInputStream();

if (url_con.getResponseCode() != 200){
return "请求网络失败";
}

BufferedReader rd = new BufferedReader(new InputStreamReader(in));
// strs = rd.readLine(); 读一行有时不一定能读得完
do { //这里要是用while的话,有时第一个json的"["又会丢掉,这里最好是用do……while循环
sbd.append(rd.readLine());
} while (rd.read()!=-1);
strs = sbd.toString();
Log.i("lan","+="+strs);
} catch (MalformedURLException e) {

Log.i("lan","MalformedURLException e"+e.toString());
e.printStackTrace();
} catch (IOException e) {

Log.i("lan","IOException e="+e.toString());
e.printStackTrace();
}
return strs;
}


从服务器返回来的是json格式如下
{
"isopen": "1",
"data": {
"bft_mobile": "95013212345678",
"mobile": "12345678910",
"package_id": "3",
"level_id": {
"type": "0",
"leveled": "3"
},
"call_limits": "3",
"call_people": "8",
"reward_time": "1",
"level_name": "黄钻用户",
"level_icon": "yellow",
"reward": "1869",
"room_status": "0",
"yunid": "123456789",
"password": "123456"
}
}
在程序中这样解析:
String s =
json = new JSONObject(s);
int id = json.getInt("isopen");
// 0表示未开通
if (id == 0) {
// 发一个注册的消息到主线程
msg = new Message();
msg.what = 5;
handler.sendMessage(msg);
return;
}
JSONObject json1 = json.getJSONObject("data");
pid = json1.getString("package_id");
phoneNum = json1.getString("mobile");
bfhId = json1.getString("bft_mobile");
call_limits = json1.getString("call_limits"); //1呼入 2呼出权限 3//呼入呼出权限
call_people = json1.getString("call_people"); //可呼多少人
reward_time = json1.getString("reward_time"); //多少倍奖励
level_name = json1.getString("level_name"); //用户等级名称
level_icon = json1.getString("level_icon"); //用户等级图标
reward = json1.getString("reward"); //奖励分钟数
password = json1.getString("password");
room_status = json1.getString("room_status");// 0 , 1, 2
//获取会议状态bfh_stautsv1.PHP 的三种值 off 已结束或未发起 on 已发起 err 异常错误

JSONObject json2 = json1.getJSONObject("level_id");
type = json2.getString("type"); //0包月,1,一次性
leveled = json2.getString("leveled");

数组的格式
[
{
"package_id": "8",
"open": "0",
"type": "0",
"call_number": "3",
"level_icon": "free",
"level_name": "免费用户",
"tariff": "0",
"call_limits": "2",
"reward_time": "1"
},

{ "package_id": "8","open": "0","type": "0","call_number": "3","level_icon": "free","level_name": "免费用户","tariff": "0","call_limits": "2","reward_time": "1" },] JSONArray resultArray = new JSONArray(s); for (int i = 0; i < resultArray.length(); i++) { JSONObject json = resultArray.getJSONObject(i); Log.i("lan","resolveJson json ==" + json); type = json.getString("type");//0包月,1,一次性 call_limits = json.getString("call_limits");//呼入呼出权限 call_people = json.getString("call_number");//可呼多少人 reward_time = json.getString("reward_time");//多少倍奖励 level_icon = json.getString("level_icon");//用户等级图标 level_name = json.getString("level_name");//用户等级名称 tariff = json.getString("tariff"); //开通多少钱

猜你在找的Json相关文章