zoukankan      html  css  js  c++  java
  • [二]Json-lib的用法

    1.Json字符串

    PrintWriter out=response.getWriter();
    // String resultJson="{"name":"张三","age":22}";
    JSONObject resultJson=new JSONObject();
    resultJson.put("name", "张三");
    resultJson.put("age", 22);
    out.println(resultJson);
    out.flush();
    out.close();

    2.JsonArray

    PrintWriter out=response.getWriter();
    JSONObject resultJson=new JSONObject();
    JSONArray jsonArray=new JSONArray();
    JSONObject jsonObject1=new JSONObject();
    jsonObject1.put("name", "张三");
    jsonObject1.put("age", 22);
    JSONObject jsonObject2=new JSONObject();
    jsonObject2.put("name", "李四");
    jsonObject2.put("age", 23);
    JSONObject jsonObject3=new JSONObject();
    jsonObject3.put("name", "王五");
    jsonObject3.put("age", 24);
    jsonArray.add(jsonObject1);
    jsonArray.add(jsonObject2);
    jsonArray.add(jsonObject3);

    resultJson.put("students", jsonArray);
    out.println(resultJson);
    out.flush();
    out.close();

    3.JsonNested

    PrintWriter out=response.getWriter();
    JSONObject resultJson=new JSONObject();
    JSONArray jsonArray=new JSONArray();
    JSONObject jsonObject1=new JSONObject();
    jsonObject1.put("name", "张三");
    jsonObject1.put("age", 22);

    JSONObject scoreObject1=new JSONObject();
    scoreObject1.put("chinese", 90);
    scoreObject1.put("math", 100);
    scoreObject1.put("english", 80);
    jsonObject1.put("score", scoreObject1);

    JSONObject jsonObject2=new JSONObject();
    jsonObject2.put("name", "李四");
    jsonObject2.put("age", 23);

    JSONObject scoreObject2=new JSONObject();
    scoreObject2.put("chinese", 70);
    scoreObject2.put("math", 90);
    scoreObject2.put("english", 90);
    jsonObject2.put("score", scoreObject2);

    JSONObject jsonObject3=new JSONObject();
    jsonObject3.put("name", "王五");
    jsonObject3.put("age", 24);

    JSONObject scoreObject3=new JSONObject();
    scoreObject3.put("chinese", 80);
    scoreObject3.put("math", 60);
    scoreObject3.put("english", 90);
    jsonObject3.put("score", scoreObject3);

    jsonArray.add(jsonObject1);
    jsonArray.add(jsonObject2);
    jsonArray.add(jsonObject3);

    resultJson.put("students", jsonArray);
    out.println(resultJson);
    out.flush();
    out.close();

  • 相关阅读:
    LeetCode 2 -- Add Two Numbers
    LeetCode 1 -- Two Sum
    LeetCode189——Rotate Array
    Win10下IIS配置 C#项目的部署与发布
    Linux查看进程和删除进程
    使用 Visual Studio 将 ASP.NET Core 应用发布到 Linux 上的应用服务
    Spring Boot 设置启动时路径和端口号
    Linux平台部署.net Core SDK
    C#教程之如何在centos操作系统上发布.net core的项目
    Linux如何查看和控制进程
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/5128813.html
Copyright © 2011-2022 走看看