Android---创建Json格式数据

本文将教你学会如何创建Json格式数据

代码如下所示:

try{

     //构建一个json数据

     JSONObject root=newJSONObject();

     root.put("title","json数据解析");

    //构建一个json数据

    //{"id":"123","username":"xiaoming"}

     JSONObject json1=newJSONObject();

     json1.put("id","123");

     json1.put("username","xiaoming");

     //构建一个json数据

     //{"id":"124","username":"xiaohong"}

     JSONObject json2=newJSONObject();

     json2.put("id","124");

    json2.put("username","xiaohong");

    //构建一个json数据

    //{"id":"125","username":"xiaozhang"}

     JSONObject json3=newJSONObject();

     json3.put("id","125");

    json3.put("username","xiaozhang");

     //构建一个数组数据,把json1,json2,json3存入数组中

    JSONArray array=newJSONArray();

    array.put(json1);

    array.put(json2);

    array.put(json3);

    //把数组加入根json数据中

    root.put("human", array);

    //打印出数据

    System.out.println("json数据为:"+root.toString());

    }catch(JSONException e) {

    //TODOAuto-generated catch block

    e.printStackTrace();

}

打印结果如下图所示:

图1-1

本文由“你正哥来了”学习过程中笔记整理,纯属个人笔记资料,如有错误,请指教,望相互学习(Android ---你正哥来了)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容