zoukankan      html  css  js  c++  java
  • 【数据解析入门系列】-初探Json

    定义

    JSON是一种轻量级的数据交换格式,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于阅读和编写,同时也易于解析和生成,并有效地提升网络传输效率。

    语法

    数据在名称/值对中

    数据由逗号分隔

    花括号保存对象

    方括号保存数组

    {
      "employees": [
        { "firstName":"John" , "lastName":"Doe" },
        { "firstName":"Anna" , "lastName":"Smith" },
        { "firstName":"Peter" , "lastName":"Jones" }
      ]
    }

     实例分析

    public static void jsonChange() throws IOException {
            
            /**
             * 方式1:传统方式 org.json
             * Json数据转换成实体对象存在缺陷,性能和功能不够完善
             */
            // JSONObject/JSONArray与String相互转换
            String jsonStr = "{"age":25,"name":"小明","sex":"男"}";
            JSONObject jsonObject = new JSONObject(jsonStr);
            System.out.println("name="+jsonObject.getString("name"));
            System.out.println("age="+jsonObject.getInt("age"));
            System.out.println("sex="+jsonObject.getString("sex"));
            System.out.println(jsonObject.toString());
            jsonStr = "[{"age":25,"name":"小明","sex":"男"},{"age":20,"name":"小红","sex":"女"}]";
            JSONArray jsonArray = new JSONArray(jsonStr);
            System.out.println(jsonArray.toString());
            for (int i=0; i<jsonArray.length();i++){
                jsonObject = (JSONObject) jsonArray.get(i);
                System.out.println(jsonObject.toString());
            }
    
            // 对象转JSONObject/JSONArray(org.json是不支持JSONObject/JSONArray转对象)
            jsonObject = new JSONObject(new Person("小明",25,"男"));
            System.out.println(jsonObject);
            List<Person> list = new ArrayList<>();
            list.add(new Person("小明",25,"男"));
            jsonArray = new JSONArray(list);
            System.out.println(jsonArray.toString());
    
            /**
             * 方式2:fastjson
             * 复杂的实体类转换成Json数据存在缺陷,解析json的速度最优
             */
            // String转对象
            jsonStr = "{"age":25,"name":"小明","sex":"男"}";
            Person person = JSON.parseObject(jsonStr,Person.class);
            System.out.println(person.toString());
            jsonStr = "[{"age":25,"name":"小明","sex":"男"},{"age":20,"name":"小红","sex":"女"}]";
            list = JSON.parseArray(jsonStr,Person.class);
            System.out.println(list.toString());
    
            // 对象转String
            jsonStr = JSON.toJSONString(person);
            System.out.println(jsonStr);
            jsonStr = JSON.toJSONString(list);
            System.out.println(jsonStr);
    
            /**
             * 方式3:Jackson
             * 复杂的Json数据转换成实体类存在缺陷,性能和功能优于传统方式
             */
            // String转对象
            ObjectMapper mapper=new ObjectMapper();
            jsonStr = "{"age":25,"name":"小明","sex":"男"}";
            person = mapper.readValue(jsonStr, Person.class);
            System.out.println(person.toString());
            jsonStr = "[{"age":25,"name":"小明","sex":"男"},{"age":20,"name":"小红","sex":"女"}]";
            list =  mapper.readValue(jsonStr,mapper.getTypeFactory().constructParametricType(ArrayList.class, Person.class));
            System.out.println(list.toString());
    
            // 对象转String
            jsonStr = mapper.writeValueAsString(person);
            System.out.println(jsonStr);
            jsonStr = mapper.writeValueAsString(list);
            System.out.println(jsonStr);
    
            /**
             * 方式4:Gson
             * 功能方面在几种方式中最优,性能方面不如Jackson方式
             */
            // String转对象
            String json = "{"age":25,"name":"小明","sex":"男"}";
            Gson gson = new Gson();
            person = gson.fromJson(json,Person.class);
            System.out.println(person.toString());
            json = "[{"age":25,"name":"小明","sex":"男"},{"age":20,"name":"小红","sex":"女"}]";
            list = gson.fromJson(json,new TypeToken<ArrayList<Person>>(){}.getType());
            System.out.println(list.toString());
    
            // 对象转String
            json = gson.toJson(person);
            System.out.println(json);
            json = gson.toJson(list);
            System.out.println(json);
        }
  • 相关阅读:
    Ubuntu 16.04下SecureCRT无法输入中文的解决思路
    Ubuntu 16.04安装SecureCRT替代XShell
    SVN提交时报错:Commit blocked by pre-commit hook (exit code 1) with no output.
    Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n
    Spring Cloud ZooKeeper集成Feign的坑1,错误:Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.
    Ubuntu 16.04 GNOME下解决Sublime Text3中文输入(ibus)(转)
    Spring Cloud简介/版本选择/ZooKeeper例子搭建简单说明
    Maven奇怪的问题,当找不到Maven输出的提示错误时可以试下这个方法
    Ubuntu 16.04系统启动时卡在:(initramfs)
    Eclipse创建Maven多模块工程
  • 原文地址:https://www.cnblogs.com/ysdrzp/p/9895170.html
Copyright © 2011-2022 走看看