//封装一个json对象,形如
/**
*{
"imei":"13223354",#每个手机的唯一识别码
"query":{
"method":"post_sms",#初始化同步
"received_sms":[{...},{...},{...},...]
}
}
*/
try {
JSONObject requestJson = new JSONObject();
//第一层imei
requestJson.put("imei", imeistring);
//第一层query
JSONObject requestQuery = new JSONObject();
//第二层query|method
requestQuery.put("method","init_co");
//第二层query|received_sms
JSONArray smsArray = new JSONArray();
JSONObject sm1 = new JSONObject();
sm1.put("key", "1");
JSONObject sm2 = new JSONObject();
sm2.put("key", "2");
smsArray.put(sm1);
smsArray.put(sm2);
requestQuery.put("received_sms", smsArray);
//第二层
requestJson.put("query", requestQuery);
SmsInfo smsinfo = new SmsInfo();
smsinfo.setSms(requestJson.toString());
infos.add(smsinfo);
}
catch (Exception e){
SmsInfo smsinfo = new SmsInfo();
smsinfo.setSms("json obejct fail");
infos.add(smsinfo);
}
//解析一个json字符串
{
String JSON =
"{" +
" \"phone\" : [\"12345678\", \"87654321\"]," +
" \"name\" : \"yuanzhifei89\"," +
" \"age\" : 100," +
" \"address\" : { \"country\" : \"china\", \"province\" : \"jiangsu\" }," +
" \"married\" : false," +
"}";
try {
JSONTokener jsonParser = new JSONTokener(JSON);
// 此时还未读取任何json文本,直接读取就是一个JSONObject对象
JSONObject person = (JSONObject) jsonParser.nextValue();
// 接下来的就是JSON对象的操作了
person.getJSONArray("phone");
person.getString("name");
person.getInt("age");
person.getJSONObject("address");
person.getBoolean("married");
} catch (Exception e) {
// 异常处理代码
}
}
android app 将对方封装从json以及从json解析出对象
猜你喜欢
转载自blog.csdn.net/u011539200/article/details/81208877
今日推荐
周排行