zoukankan      html  css  js  c++  java
  • Java中创建Json对象

    // 声明两个json数组  
    [java] view plain copy print?
          JSONArray gResTable = new JSONArray();   
          JSONArray gCmtTable = new JSONArray();   
    // 声明json对象       
    ONObject outData = new JSONObject();  
    //把json数组加到json对象中  
    [java] view plain copy print?
    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格式如下:  
    [java] view plain copy print?
    /* 
        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"} 
     ]}; 
    */  

    为方便学习,本文章摘自http://blog.csdn.net/linxinyuluo/article/details/7605338

  • 相关阅读:
    IOS 开发 网络发展史(基础知识)
    加密详解
    IOS对接支付的流程
    App混合开发浅谈
    swift语法100
    2015年最新Android基础入门教程目录第二章:Android UI(User Interface)详解(已完结 40/40)
    2015年最新Android基础入门教程目录第一章:环境搭建与开发相关(已完结 10/10)
    Reactive开发
    tensorflow 安装
    Mask RCNN笔记
  • 原文地址:https://www.cnblogs.com/Silencewen/p/7471085.html
Copyright © 2011-2022 走看看