[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 {
// Generated JSON data 1
//{
// "Phones": ["028-123456", "15002806555"], // JSON Array
// "Name": "Xiaoqiang", // string
// "Age": 17, // Value
// "Address": {"country": "china", "province": "Sichuan"}, // JSON object
// "Married": false // Boolean Value
//}
// The generated JSON data 2
//
//{
// "Api": "categories ",
// "Count": "3 ",
// "Data ":[
//{
// "Category_id": "1 ",
// "Category_name": "category ",
// "Category_rgb": "FFFFFF ",
// "Category_news_count": "0"
//},
//{
// "Category_id": "2 ",
// "Category_name": "category has been selected successfully ",
// "Category_rgb": "FFFFFF ",
// "Category_news_count": "0"
//},
Www.2cto.com // "category_id": "3 ",
// "Category_name": "social ",
// "Category_rgb": "FFFFFF ",
// "Category_news_count": "113"
//}
//]
//}
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Try {
// Generate JSON data 1
// First, the outermost layer
JSONObject resultJsonObject = new JSONObject ();
// Phones is an array, so JSONArray is created.
JSONArray phonesJsonArray = new JSONArray ();
// JSONArray saves data
PhonesJsonArray. put ("028-123456"). put ("15002806555 ");
// Save the outermost layer
ResultJsonObject. put ("phones", phonesJsonArray );
// Save the outermost layer
ResultJsonObject. put ("name", "Xiaoqiang ");
// Save the outermost layer
ResultJsonObject. put ("age", 17 );
// Address is an object, so JSONObject is created.
JSONObject addressJSONObject = new JSONObject ();
AddressJSONObject. put ("country", "china ");
AddressJSONObject. put ("province", "Sichuan ");
// Save the outermost layer
ResultJsonObject. put ("address", addressJSONObject );
// Save the outermost layer
ResultJsonObject. put ("married", false );
System. out. println ("resultJsonObject. toString () =" + resultJsonObject. toString ());
System. out. println ("--------------------------------------------------");
// Parse 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 ("--------------------------------------------------");
// Generate JSON data 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 ("---------------------------------");
// Parse 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;
}
}