zoukankan      html  css  js  c++  java
  • 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"}
  • 相关阅读:
    POJ 2000 Gold Coins
    HDU 5804 Price List
    POJ 1316 Self Numbers
    HDU 5783 Divide the Sequence
    rabbitmq基础使用
    centos7安装RabbitMQ
    编程之路┊一个程序员走过的路
    jquery layer弹出层插件
    SWFUpload 2.5.0版 官方说明文档 中文翻译版
    C# 格式化字符串,日期,字符串操作汇总
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7266718.html
Copyright © 2011-2022 走看看