java 建立json對象

來源:互聯網
上載者:User
   // 聲明兩個json數組
         JSONArray gResTable = new JSONArray();          JSONArray gCmtTable = new JSONArray();    // 聲明json對象JSONObject outData = new JSONObject();   //把json數組加到json對象中
outData.put("ResTable", gResTable);outData.put("CmtTable", gCmtTable);   //此時建立出來的如下:jsonData={"ResTable":[],"CmtTable":[]};   把json資料加到json數組中for (int i = 0; i < 2; i ++){JSONObject node = new JSONObject();node.put("thumb_path", "./Image/" + i +".gif");node.put("flash_path", "./Image/" + i +".gif");node.put("desc1", "可疑車輛" + i);node.put("desc2", "");node.put("desc3", "");node.put("desc4", "");node.put("title", "hello");node.put("upload_time", (new java.util.Date()).toString());node.put("uploader", "王二");gResTable.put(node);}for (int i = 0; i < 2; i ++){JSONObject node = new JSONObject();node.put("logo_path", "./Image/" + i +".gif");node.put("comment", "hello");node.put("comment_time", (new java.util.Date()).toString());node.put("author", "王二");gCmtTable.put(node);}建立出來的json格式如下:
/*    jsonData={"ResTable":[{"upload_time":"Sat May 26 20:16:37 CST 2012","title":"hello","flash_path":"./Image/0.gif","uploader":"王二","thumb_path":"./Image/0.gif","desc3":"","desc4":"","desc1":"可疑車輛0","desc2":""},{"upload_time":"Sat May 26 20:16:37 CST 2012","title":"hello","flash_path":"./Image/1.gif","uploader":"王二","thumb_path":"./Image/1.gif","desc3":"","desc4":"","desc1":"可疑車輛1","desc2":""}],    "CmtTable":[{"author":"王二","logo_path":"./Image/0.gif","comment":"hello","comment_time":"Sat May 26 20:16:37 CST 2012"},{"author":"王二","logo_path":"./Image/1.gif","comment":"hello","comment_time":"Sat May 26 20:16:37 CST 2012"} ]};*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.