package com.example.testcreatejson; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.view.Menu; public class MainActivity extends Activity { // 生成的JSON数据1 // { // "phones" : ["028-123456", "15002806555"], //JSON数组 // "name" : "小强", // 字符串 // "age" : 17, // 数值 // "address" : { "country" : "china", "province" : "四川" }, // JSON对象 // "married" : false // 布尔值 // } // 生成的JSON数据2 // // { // "api":"categories", // "count":"3", // "data":[ // { // "category_id":"1", // "category_name":"ヘッドライン", // "category_rgb":"FFFFFF", // "category_news_count":"0" // }, // { // "category_id":"2", // "category_name":"ランキング", // "category_rgb":"FFFFFF", // "category_news_count":"0" // }, // { // "category_id":"3", // "category_name":"社会", // "category_rgb":"FFFFFF", // "category_news_count":"113" // } // ] // } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { //生成JSON数据1 //首先是最外层 JSONObject resultJsonObject=new JSONObject(); //phones是一个数组,所以创建JSONArray JSONArray phonesJsonArray=new JSONArray(); //JSONArray保存数据 phonesJsonArray.put("028-123456").put("15002806555"); //最外层保存 resultJsonObject.put("phones", phonesJsonArray); //最外层保存 resultJsonObject.put("name", "小强"); //最外层保存 resultJsonObject.put("age", 17); //address是一个对象,所以创建JSONObject JSONObject addressJSONObject=new JSONObject(); addressJSONObject.put("country", "china"); addressJSONObject.put("province", "四川"); //最外层保存 resultJsonObject.put("address", addressJSONObject); //最外层保存 resultJsonObject.put("married", false); System.out.println(" resultJsonObject.toString()="+resultJsonObject.toString()); System.out.println("--------------------------------------------------"); //解析JSON1 boolean married=resultJsonObject.getBoolean("married"); JSONObject address=resultJsonObject.getJSONObject("address"); String country=address.getString("country"); String province=address.getString("province"); int age=resultJsonObject.getInt("age"); String name=resultJsonObject.getString("name"); JSONArray phones=resultJsonObject.getJSONArray("phones"); String phoneNumber1=phones.getString(0); String phoneNumber2=phones.getString(1); System.out.println("married="+married+",country="+country+",province="+province+",age="+age+",name="+ name+",phoneNumber1="+phoneNumber1+",phoneNumber2="+phoneNumber2); System.out.println("--------------------------------------------------"); //生成JSON数据2 JSONObject jsonObject=new JSONObject(); jsonObject.put("api", "categories"); jsonObject.put("count", "3"); JSONArray dataJsonArray=new JSONArray(); JSONObject dataJsonObject1=new JSONObject(); dataJsonObject1.put("category_id", "1"); dataJsonObject1.put("category_name", "name1"); dataJsonObject1.put("category_rgb", "rgb1"); dataJsonObject1.put("category_news_count", "1"); dataJsonArray.put(dataJsonObject1); JSONObject dataJsonObject2=new JSONObject(); dataJsonObject2.put("category_id", "2"); dataJsonObject2.put("category_name", "name2"); dataJsonObject2.put("category_rgb", "rgb2"); dataJsonObject2.put("category_news_count", "2"); dataJsonArray.put(dataJsonObject2); JSONObject dataJsonObject3=new JSONObject(); dataJsonObject3.put("category_id", "3"); dataJsonObject3.put("category_name", "name3"); dataJsonObject3.put("category_rgb", "rgb3"); dataJsonObject3.put("category_news_count", "3"); dataJsonArray.put(dataJsonObject3); jsonObject.put("data", dataJsonArray); System.out.println("jsonObject.toString()="+jsonObject.toString()); System.out.println("---------------------------------"); //解析JSON2 String api=jsonObject.getString("api"); String count=jsonObject.getString("count"); JSONArray datas=jsonObject.getJSONArray("data"); for (int i = 0; i < datas.length(); i++) { JSONObject everyDataJSONObject=datas.getJSONObject(i); String category_id=everyDataJSONObject.getString("category_id"); String category_name=everyDataJSONObject.getString("category_name"); String category_rgb=everyDataJSONObject.getString("category_rgb"); String category_news_count=everyDataJSONObject.getString("category_news_count"); System.out.println("category_id="+category_id+",category_name="+category_name+ ",category_rgb="+category_rgb+",category_news_count="+category_news_count); System.out.println("----------------------------------"); } } catch (Exception e) { e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }