json的格式
1.{name:"张三:",age:21,sex:"女"}
JSONObject demoJson = new JSONObject(jsonString);
String name = demoJson.getString("name");
String age = demoJson.getString("age");
String sex = demoJson.getString("sex");
2.[{name:"张三:",age:21,sex:"女"},{name:"李斯",age:21,sex:"女"},{name:"王五",age:21,sex:"女"}]
JSONArray jsonary = new JSONArray(jsonString);
ArrayList<myjson> objlist = new ArrayList<myjson>();
for (int i = 0; i < jsonary.length(); i++) {
my = new myjson();
JSONObject jsonobj = jsonary.getJSONObject(i);
my.set_name(jsonobj.getString("name"));
my.set_age(jsonobj.getInt("age"));
my.set_sex(jsonobj.getString("sex"));
objlist.add(my);
}
3.{singer:{id:1,name:"tom",gender:"男"}}
JSONObject jsonObj = new JSONObject(jsonString).getJSONObject("singer");
int id = jsonObj.getInt("id");
String name = jsonObj.getString("name");
String gender = jsonObj.getString("gender");
4.{singers:[{'id':02,'name':'tom','gender':'男'},{'id':05,'name':'lily,'gender':'女'}]}
JSONArray jsonObjs = new JSONObject(jsonString).getJSONArray("singers");
String s = "";
for(int i = 0; i < jsonObjs.length() ; i++){
JSONObject jsonObj = ((JSONObject)jsonObjs.opt(i)).getJSONObject("singer");
int id = jsonObj.getInt("id");
String name = jsonObj.getString("name");
String gender = jsonObj.getString("gender");
s += "ID号"+id + ", 姓名:" + name + ",性别:" + gender+ "\n" ;
}
5.["A区 ","B区 ","C区 ","D区 "]
JSONArray jsonary = new JSONArray(jsonString);
String s= "";
for (int i = 0; i < jsonary.length(); i++) {
s += jsonary.getString(i);
}
1.{name:"张三:",age:21,sex:"女"}
JSONObject demoJson = new JSONObject(jsonString);
String name = demoJson.getString("name");
String age = demoJson.getString("age");
String sex = demoJson.getString("sex");
2.[{name:"张三:",age:21,sex:"女"},{name:"李斯",age:21,sex:"女"},{name:"王五",age:21,sex:"女"}]
JSONArray jsonary = new JSONArray(jsonString);
ArrayList<myjson> objlist = new ArrayList<myjson>();
for (int i = 0; i < jsonary.length(); i++) {
my = new myjson();
JSONObject jsonobj = jsonary.getJSONObject(i);
my.set_name(jsonobj.getString("name"));
my.set_age(jsonobj.getInt("age"));
my.set_sex(jsonobj.getString("sex"));
objlist.add(my);
}
3.{singer:{id:1,name:"tom",gender:"男"}}
JSONObject jsonObj = new JSONObject(jsonString).getJSONObject("singer");
int id = jsonObj.getInt("id");
String name = jsonObj.getString("name");
String gender = jsonObj.getString("gender");
4.{singers:[{'id':02,'name':'tom','gender':'男'},{'id':05,'name':'lily,'gender':'女'}]}
JSONArray jsonObjs = new JSONObject(jsonString).getJSONArray("singers");
String s = "";
for(int i = 0; i < jsonObjs.length() ; i++){
JSONObject jsonObj = ((JSONObject)jsonObjs.opt(i)).getJSONObject("singer");
int id = jsonObj.getInt("id");
String name = jsonObj.getString("name");
String gender = jsonObj.getString("gender");
s += "ID号"+id + ", 姓名:" + name + ",性别:" + gender+ "\n" ;
}
5.["A区 ","B区 ","C区 ","D区 "]
JSONArray jsonary = new JSONArray(jsonString);
String s= "";
for (int i = 0; i < jsonary.length(); i++) {
s += jsonary.getString(i);
}