產生JSON資料

來源:互聯網
上載者: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 {// 產生的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是一個對象,所以建立JSONObjectJSONObject 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("--------------------------------------------------");//解析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("--------------------------------------------------");//產生JSON資料2JSONObject 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("---------------------------------");//解析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;    }               }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.