zoukankan      html  css  js  c++  java
  • 初识JSON

    官方文档:http://www.json.org.cn/index.htm

    JSON在线编辑器:https://jsoneditoronline.org/

    Json是一种与开发语言无关、轻量级的数据格式。全称JavaScript Object NOtation 

    • 优点:

    易于人的阅读和编写,易于程序的解析与生产

    • 数据结构-Object

    使用花括号{}包含的键值对结构,Key必须是string类型,value为任何基本类型或数据结构

    • 数据结构-Array

    使用中括号[]来起始和结束,并用逗号来分割元素

    • 基本类型
    string、number、truefalsenull
    {
    "name":"王小二",
    "age":25,
    "birthday":"1990-01-01",
    "school":"蓝翔",
    "major":["理发","挖掘机"],
    "has_girlfriend":false,
    "car":null,
    "house":null,
    "comment":"这是一个注释"
    }

    一、引入json依赖

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20090211</version>
    </dependency>

    二、测试JSONObject使用put方法创建json 数据

    @Test
        public void testJSONObject(){
            JSONObject jsonObject = new JSONObject();
            Object nullObj = null;
            try {
                jsonObject.put("name","王小二");
                jsonObject.put("age",25);
                jsonObject.put("birthday","1990-01-01");
                jsonObject.put("shcool","蓝翔");
                jsonObject.put("major",new String[]{"理发","挖掘机"});
                jsonObject.put("has_girlfriend",false);
                jsonObject.put("car","null");
                jsonObject.put("house",nullObj);
                jsonObject.put("comment","这是一个注释");
                System.out.print(jsonObject);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    生成json格式内容

    {
      "birthday": "1990-01-01",
      "major": [
        "理发",
        "挖掘机"
      ],
      "name": "王小二",
      "has_girlfriend": false,
      "comment": "这是一个注释",
      "shcool": "蓝翔",
      "age": 25
    }

    注意:key为null的值不会显示。由此可见,如果put一个NUll值给JSON,JSON会自动屏蔽此key不会有任何处理

    @Test
    public void main(String[] args) { 
      JSONObject json = new JSONObject(); 
      String value = "null"; 
      json.put("key", value); 
      System.out.println(json); 
    } 

    控制台输出:{"key":null}

      我们将String类型的字符串value的值设置为“null”。此时JSON会带有key及value存在。大家请注意看value为null。并不是“null”不带引号。我猜测应该是JSON自动将字符串“null”转换为可识别的空值。如果我们将value的值设置为非“null”的任何字符,JSON都会带有引号。

      综上所述:如果需要在value为空时不显示key,则可以直接传递null。但是如果需要不管在value是否为空时都带有key则需要手动将null转换为“null”字符串即可。

    三、使用Map构建JSON

    @Test
    public void creatJsonByMap(){
      Map<String ,Object> map = new HashMap<>();
      map.put("name","王小二");
      map.put("age",25);
      map.put("birthday","1990-01-01");
      map.put("shcool","蓝翔");
      map.put("major",new String[]{"理发","挖掘机"});
      map.put("has_girlfriend",false);
      map.put("car",null);
      map.put("house",null);
      map.put("comment","这是一个注释");
      JSONObject jsonObject = new JSONObject(map);
    System.out.print(jsonObject);
    }

    控制台输出:

    {
      "birthday": "1990-01-01",
      "major": [
        "理发",
        "挖掘机"
      ],
      "car": "null",
      "name": "王小二",
      "has_girlfriend": false,
      "comment": "这是一个注释",
      "shcool": "蓝翔",
      "age": 25
    }

    注意 null值有输出。通过map构建JSON时,null值可以显示。

    四、使用JavaBean构建对象

    @Test
    public void creatJsonByBean() throws ParseException {
    
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
      Date date = simpleDateFormat.parse("1990-09-01");
      String [] major ={"理发","挖掘机"};
    
      Customer customer = new Customer(null,25,date,false,major,"王小二","蓝翔");
      JSONObject jsonpObject = new JSONObject(customer);
      System.out.print(jsonpObject);
    
    }

    控制台输出:

    "birthday":"Mon Jan 01 00:09:00 CST 1990","address":null,"major":[{"bytes":[{},{},{},{},{},{}],"empty":false},{"bytes":[{},{},{},{},{},{},{},{},{}],"empty":false}],"school":"蓝翔","name":"王小二","has_girlfriend":false,"age":25}

    出现”major”:[{“bytes”:[{},{},{},{},{},{}],”empty”:false}的可以换个org.json的新版本,亲测可用

    {"birthday":"Mon Jan 01 00:09:00 CST 1990","major":["理发","挖掘机"],"school":"蓝翔","name":"王小二","has_girlfriend":false,"age":25}

    五、从文件中读取json

    @Test
    public void readJsonFromFile() throws IOException {
      File file = new File(JsonObjectSample.class.getResource("/customer.json").getFile());
      String content = FileUtil.readAsString(file);
      JSONObject jsonObject = new JSONObject(content);
    
      boolean has_girlfriend = jsonObject.getBoolean("has_girlfriend");
    
      String name = jsonObject.getString("name");
    
      Integer age = jsonObject.getInt("age");
    
      JSONArray major = jsonObject.getJSONArray("major");
    
      System.out.print(major);
    }
  • 相关阅读:
    获取资源文件 r.drawable中的图片转换为drawable、bitmap
    Android 启动白屏或者黑屏闪现解决
    Android应用截图方法
    史上最全的变量、作用域和内存问题
    RunLoop总结:RunLoop的应用场景(四)
    poj3436 ACM Computer Factory, 最大流,输出路径
    android开发——从相冊中选择图片不裁剪
    <html>
    poj3073
    poj 2482 Stars in Your Window (线段树扫描线)
  • 原文地址:https://www.cnblogs.com/realshijing/p/8215694.html
Copyright © 2011-2022 走看看