//{"id":2,"sex":"男","name":"小黑"}
JSONObject worker1 = new JSONObject();
worker1.put("id", 2);
worker1.put("name", "小黑");
worker1.put("sex","男");
String sWorker1 = worker1.toString();
String sName = worker1.getString("name");
//{"id":2,"address":{"province":"jiangsu","country":"china"},"sex":"男","name":"小黑"}
JSONObject address = new JSONObject();
address.put("country", "china");
address.put("province", "jiangsu");
worker1.put("address", address);
String worker2 = worker1.toString();
String sAddress = worker1.getString("address");
JSONObject addressObj = new JSONObject(sAddress);
String sCountry = addressObj.getString("country");
//{"worker":[{"id":3,"name":"小明"},{"id":4,"name":"小红"}]}
JSONObject company = new JSONObject();
company.put("worker", "worker3");
JSONObject worker3 = new JSONObject();
worker3.put("id", 3);
worker3.put("name","小明");
JSONObject worker4 = new JSONObject();
worker4.put("id", 4);
worker4.put("name","小红");
JSONArray workerArray = new JSONArray();
workerArray.put(worker3);
workerArray.put(worker4);
company.put("worker", workerArray);
String sCompany = company.toString();
JSONArray jsonObjs = new JSONObject(sCompany).getJSONArray("worker");
for(int i=0; i<jsonObjs.length(); i++)
{
JSONObject workObj = jsonObjs.getJSONObject(i);
int iId = workObj.getInt("id");
String sName4 = workObj.getString("name");
}
Android JSON解析
最新推荐文章于 2024-05-23 09:23:37 发布