[java]
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"
// },
www.2cto.com// "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;
}
}