码迷,mamicode.com
首页 > 移动开发 > 详细

Android创建JSON格式数据

时间:2014-10-23 14:27:01      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:android   json   数据   

Android创建JSON格式数据

作为上一篇博客的补充,简单那讲解了一下Android创建JSON格式数据的小Demo。

1. 创建JSON格式数据

对于Android创建JSON格式数据,由于Android官方提供了相应了put(key, value)等方法,因此代码十分简单,如下所示:

JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONObject object_1 = new JSONObject();
JSONObject object_2 = new JSONObject();
JSONObject object_3 = new JSONObject();

try {
    object_1.put("id", 1);
    object_1.put("ide", "eclipse");
    object_1.put("name", "Java");
    object_2.put("id", 2);
    object_2.put("ide", "XCode");
    object_2.put("name", "Swift");
    object_3.put("id", 3);
    object_3.put("ide", "Visual Studio");
    object_3.put("name", "C#");

    jsonArray.put(object_1);
    jsonArray.put(object_2);
    jsonArray.put(object_3);

    jsonObject.put("languages", jsonArray);
    jsonObject.put("cat", "it");

} catch (JSONException e) {
    e.printStackTrace();
}

Log.i("TESTJSON", jsonObject.toString());

最后打印出来的Log日志信息为:

{"languages":[{"id":1,"ide":"eclipse","name":"Java"},{"id":2,"ide":"XCode","name":"Swift"},{"id":3,"ide":"Visual Studio","name":"C#"}],"cat":"it"}

Android创建JSON格式数据

标签:android   json   数据   

原文地址:http://blog.csdn.net/monkeyzsf/article/details/40396241

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!