Generate JSON data

Source: Internet
Author: User
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 // generated JSON data 2 // {// "api": "categories", // "count": "3 ", // "data": [// {// "category_id": "1", // "category_name": "Too many other users ", // "category_rgb": "FFFFFF", // "category_news_count": "0" //}, // {// "category_id": "2 ", // "category_name": "category_rgb": "FFFFFF", // "category_news_count": "0 "//}, // {// "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 // The first is the outermost layer JSONObject resultJsonObject = new JSONObject (); // phones is an array, therefore, create JSONArray phonesJsonArray = new JSONArray (); // JSONArray to save the data phonesJsonArray. put ("028-123456 "). put ("15002806555"); // The outermost layer stores resultJsonObject. put ("phones", phonesJsonArray); // The outermost layer stores resultJsonObject. put ("name", "Xiaoqiang"); // The outermost layer stores resultJsonObject. put ("age", 17); // address is an object, so create JSONObjectJSONObject addressJSONObject = new JSONObject (); addressJSONObject. put ("country", "china"); addressJSONObject. put ("province", "Sichuan"); // The outermost layer stores resultJsonObject. put ("address", addressJSONObject); // save resultJsonObject at the outermost layer. put ("married", false); System. out. println ("resultJsonObject. toString () = "+ resultJsonObject. toString (); System. out. println ("------------------------------------------------"); // parse JSON1boolean 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 JSON2String 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 ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.