当前位置: 首页 > news >正文

平板网站开发河南网站推广电话

平板网站开发,河南网站推广电话,建设培训网站办安全员c证,真做av网站一、定义 JSON:就是一种轻量级的数据交换格式,被广泛应用于WEB应用程序开发。JSON的简洁和清晰的层次结构,易于阅读和编写;同时也易于机器解析和生成,有效的提升网络传输效率;支持多种语言,很多…

一、定义

JSON:就是一种轻量级的数据交换格式,被广泛应用于WEB应用程序开发。JSON的简洁和清晰的层次结构,易于阅读和编写;同时也易于机器解析和生成,有效的提升网络传输效率;支持多种语言,很多流行的语言都对JSON格式有着很友好的支持。

  • JSON对象:就是多个属性被{}括起来的。
  • JSON数组:就是包含了多个JSON对象的一个集合,数组是以数组括号[]括起来的。JSON数组并不一定是要相同的JSON对象的集合,也可以是不同的对象。

JSON、JSON对象、JSON数组的区别 

  • JSON是一种数据结构,类型xml;
  • JSON对象则是对JSON的具体体现;JSON数组则是将多个JSON对象进行存储的一个集合。

这里以fastjson2来进行讲解,不同的jar包对JSON相关的处理有着不同的实现方式,但是大部分方法也都是相同的。

二、fastjson2

2.1、fastjson2简介

fastjson2 是 FASTJSON 项目的重要升级,目标是为下一个十年提供一个高性能的JSON库, fastjson2 性能相比原先旧的 fastjson 有了很大提升,并且 fastjson2 更安全,完全删除autoType白名单,提升了安全性。

中文文档: 

​​​​https://github.com/alibaba/fastjson2/blob/main/README.md

下面是一些常用的方法 

2.2、导入fastjson2依赖

maven如下:pom(本文所有代码仅使用这一个依赖即可):

    <dependencies><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>2.0.26</version></dependency></dependencies>

需要注意的一点是在使用 fastjson2 时导入的包名是 com.alibaba.fastjson2 ,就像下面这样:

import com.alibaba.fastjson2.JSON;

import com.alibaba.fastjson2.JSONObject;

import com.alibaba.fastjson2.JSONArray;

2.3、json对象与json数组的创建

2.3.1、json对象创建

import com.alibaba.fastjson2.JSONObject;public class Demo {public static void main(String[] args) {JSONObject info = new JSONObject();info.put("name", "张三");info.put("age", "18");info.put("地理", "70");info.put("英语", "60");System.out.println(info);}
}

2.3.2、json数组创建

import com.alibaba.fastjson2.JSONArray;public class Demo {public static void main(String[] args) {JSONArray array = new JSONArray();array.add("1班");array.add("2班");array.add("3班");System.out.println(array);}
}

2.3.2、json对象添加到json数组

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;public class Demo {public static void main(String[] args) {JSONObject info1 = new JSONObject();info1.put("name", "张三");info1.put("age", "18");JSONObject info2 = new JSONObject();info2.put("name", "李四");info2.put("age", "19");//把上面创建的两个json对象加入到json数组里JSONArray array = new JSONArray();array.add(info1);array.add(info2);System.out.println(array);}
}

2.4、json对象取值与json数组遍历取值

2.4.1、json对象取值

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;public class Demo {public static void main(String[] args) {JSONArray array = new JSONArray();array.add("1班");array.add("2班");array.add("3班");JSONObject school = new JSONObject();school.put("schoolName", "第一中学");school.put("teacher", "刘梅");JSONObject info = new JSONObject();info.put("name", "张三");info.put("age", "18");info.put("gradle", array);info.put("schoolInfo", school);//从info中取值System.out.println(info.getString("name")); //张三System.out.println(info.getIntValue("age"));//18System.out.println(info.getJSONArray("gradle"));//["1班","2班","3班"]System.out.println(info.getJSONObject("schoolInfo"));//{"schoolName":"第一中学","teacher":"刘梅"}}
}

2.4.2、json数组遍历取值 

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;public class Demo {public static void main(String[] args) {JSONObject info1 = new JSONObject();info1.put("name", "张三");info1.put("age", "18");JSONObject info2 = new JSONObject();info2.put("name", "李四");info2.put("age", "19");JSONArray array = new JSONArray();array.add(info1);array.add(info2);//遍历获取json数组中对象的值for (int i = 0; i < array.size(); i++) {JSONObject json = array.getJSONObject(i);System.out.println(json.getString("name"));System.out.println(json.getString("age"));}}
}

2.5、json对象与字符串的转换 

2.5.1、json对象与字符串的转换

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;public class Demo {public static void main(String[] args) {JSONObject info = new JSONObject();info.put("name", "张三");info.put("age", "18");info.put("地理", "70");info.put("英语", "60");//JSON对象转字符串String str = JSON.toJSONString(info);System.out.println(str);//JSON字符串转JSON对象JSONObject json = JSONObject.parseObject(str);System.out.println(json);}
}

2.5.2、json字符串的字节数组转json对象

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;public class Demo {public static void main(String[] args) {String str = "{\"name\":\"张三\",\"age\":\"18\",\"地理\":\"70\",\"英语\":\"60\"}";byte[] bytes = str.getBytes();JSONObject data = JSON.parseObject(bytes);System.out.println(data);}
}

2.6、json数组与字符串的转换

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;public class Demo {public static void main(String[] args) {String text = "[\"张三\",\"李四\",\"王五\"]";System.out.println(text);//json字符串转json数组JSONArray data = JSON.parseArray(text);//json数组转json字符串String str = JSONArray.toJSONString(data);System.out.println(data);System.out.println(str);}
}

2.7、json字符串转java对象的转换 

Student类如下:

public class Student {public String name;public int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Student(String name, int age) {this.name = name;this.age = age;}
}

2.7.1、json字符串转java对象的转换

import com.alibaba.fastjson2.JSON;public class Demo {public static void main(String[] args) {Student student = new Student("张三", 18);//Student对象转JSON字符串String studentStr = JSON.toJSONString(student);//JSON字符串转Student对象Student data = JSON.parseObject(studentStr, Student.class);System.out.println(studentStr);}
}

2.7.2、java对象转byte数组转换 

import com.alibaba.fastjson2.JSON;public class Demo {public static void main(String[] args) {Student student = new Student("张三", 18);//Student对象转字节数组byte[] text = JSON.toJSONBytes(student);//字节数组转Student对象Student data = JSON.parseObject(text, Student.class);System.out.println(data.getName() + data.getAge());}
}

2.8、json字符串与Map转换 

2.8.1、json字符串转Map

@Testpublic void test05(){String str = "{\n" +"\"gradle\":\"高一\",\n" +"\"number\":\"2\",\n" +"\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +"         {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +"}";Map<String, Object> map1 = JSONObject.parseObject(str, new TypeReference<Map<String, Object>>() {});Map map2 = JSON.parseObject(str, Map.class);System.out.println(map1);System.out.println(map2);}

2.8.2、Map转json字符串

(注意:如果直接使用JSON.toJSONString(map)转换,因为"测试1"的值为null,转换的结果就会是 {“测试2”:“hello”})

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;import java.util.HashMap;
import java.util.Map;public class Demo {public static void main(String[] args) {Map<String, String> map = new HashMap<>();map.put("测试1", null);map.put("测试2", "hello");//{"测试2":"hello","测试1":null}String str = JSON.toJSONString(map, JSONWriter.Feature.WriteMapNullValue);System.out.println(str);}
}

如果你使用的是老的fastjson1(下述是简单示例,不可使用),可以像下面这样转换:

Map<String, String> map = new HashMap<>();
map.put("测试1", null);
map.put("测试2", "hello");
String str = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue) ;

2.9、json数组转List

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;import java.util.List;
import java.util.Map;public class Demo {public static void main(String[] args) {String str = "{\n" +"\"gradle\":\"高一\",\n" +"\"number\":\"2\",\n" +"\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +"         {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +"}";JSONObject strJson = JSONObject.parseObject(str);//获取people数组JSONArray people = strJson.getJSONArray("people");//json数组转ListList<Map> list = people.toJavaList(Map.class);System.out.println(str);}
}

如果你使用的是老的fastjson1,可以像下面这样转换:    

String str = "{\n" +"\"gradle\":\"高一\",\n" +"\"number\":\"2\",\n" +"\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +"         {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +"}";JSONObject strJson = JSONObject.parseObject(str);//字符串转json对象
String people = String.valueOf(strJson.getJSONArray("people"));
List<Map<String, String>> list = (List<Map<String, String>>) JSONArray.parse(people);

 

参考文章:JSONObject详解(com.alibaba)-fastjson2_com.alibaba.fastjson2-CSDN博客


文章转载自:
http://dinncoinadequateness.tpps.cn
http://dinncostaggeringly.tpps.cn
http://dinncocorkboard.tpps.cn
http://dinncosick.tpps.cn
http://dinncoazo.tpps.cn
http://dinncoaccommodative.tpps.cn
http://dinncorecrementitious.tpps.cn
http://dinncosimilarity.tpps.cn
http://dinncotailorship.tpps.cn
http://dinncotacker.tpps.cn
http://dinncodimorph.tpps.cn
http://dinncochrysanth.tpps.cn
http://dinncoschizophrenese.tpps.cn
http://dinncosandarac.tpps.cn
http://dinncoincurrent.tpps.cn
http://dinncoscolopendrine.tpps.cn
http://dinncoheathbird.tpps.cn
http://dinncohairbrained.tpps.cn
http://dinncopreediting.tpps.cn
http://dinncohyposensitive.tpps.cn
http://dinncoprofessional.tpps.cn
http://dinncolocomotive.tpps.cn
http://dinncozanzibari.tpps.cn
http://dinncowindcharger.tpps.cn
http://dinncohomeopathy.tpps.cn
http://dinncodiscomfiture.tpps.cn
http://dinncodeclaredly.tpps.cn
http://dinncoseduction.tpps.cn
http://dinncothimphu.tpps.cn
http://dinncoseminarist.tpps.cn
http://dinncohypsicephaly.tpps.cn
http://dinncoviipuri.tpps.cn
http://dinncorockman.tpps.cn
http://dinncounderdrift.tpps.cn
http://dinncocourtier.tpps.cn
http://dinncosuperplasticity.tpps.cn
http://dinncouptight.tpps.cn
http://dinncosituate.tpps.cn
http://dinncofaithlessly.tpps.cn
http://dinncotrellis.tpps.cn
http://dinncopully.tpps.cn
http://dinncoobstetric.tpps.cn
http://dinncoexsufflation.tpps.cn
http://dinncospherical.tpps.cn
http://dinncodespumation.tpps.cn
http://dinncoebullioscopy.tpps.cn
http://dinncooneparty.tpps.cn
http://dinncozincite.tpps.cn
http://dinncobehead.tpps.cn
http://dinncobroomy.tpps.cn
http://dinncocon.tpps.cn
http://dinncoinsectual.tpps.cn
http://dinncoterawatt.tpps.cn
http://dinncoeuciliate.tpps.cn
http://dinncosudra.tpps.cn
http://dinncoenlarge.tpps.cn
http://dinncoallochroic.tpps.cn
http://dinncosemiretractile.tpps.cn
http://dinncocoequally.tpps.cn
http://dinncocoxcomb.tpps.cn
http://dinncobugloss.tpps.cn
http://dinncojehad.tpps.cn
http://dinncogot.tpps.cn
http://dinncostory.tpps.cn
http://dinncoinfluxion.tpps.cn
http://dinncoboyhood.tpps.cn
http://dinncocobra.tpps.cn
http://dinncoknickknack.tpps.cn
http://dinncopiezometrical.tpps.cn
http://dinncosleugh.tpps.cn
http://dinncodemurely.tpps.cn
http://dinncotamperproof.tpps.cn
http://dinncolaevulose.tpps.cn
http://dinncoreception.tpps.cn
http://dinncocareerman.tpps.cn
http://dinncoreserpinized.tpps.cn
http://dinncosigint.tpps.cn
http://dinncouncompassionate.tpps.cn
http://dinncoshent.tpps.cn
http://dinncohankow.tpps.cn
http://dinncounmusicality.tpps.cn
http://dinncofiorin.tpps.cn
http://dinncokerplunk.tpps.cn
http://dinncoimperatival.tpps.cn
http://dinncooxytocin.tpps.cn
http://dinncopanzer.tpps.cn
http://dinncoinfarction.tpps.cn
http://dinncozeppole.tpps.cn
http://dinncofinest.tpps.cn
http://dinncoturtleburger.tpps.cn
http://dinncogoniotomy.tpps.cn
http://dinncothermionics.tpps.cn
http://dinncocheapshit.tpps.cn
http://dinncolassallean.tpps.cn
http://dinncoundo.tpps.cn
http://dinncooystershell.tpps.cn
http://dinncoinstruction.tpps.cn
http://dinnconameplate.tpps.cn
http://dinncorascally.tpps.cn
http://dinncocicatrix.tpps.cn
http://www.dinnco.com/news/135800.html

相关文章:

  • 新建的网站如何做seo自媒体营销的策略和方法
  • 产品销售推广方案网络优化报告
  • 小程序定制开发百度关键词seo优化
  • 怎样做 网站的快捷链接西安百度竞价托管
  • 综合网站建设网络营销策略理论有哪些
  • 有关师德建设的网站网址怎么创建
  • 做风险代理案源的网站济南头条今日新闻
  • 网站内部链接的作用有哪些全媒体运营师培训费用
  • 做策划有帮助的网站百度联盟推广
  • 网站建设询价单新东方烹饪培训学校
  • 怎么做cms网站广州百度推广开户
  • 江西省网站备案2020十大网络热词
  • wordpress8小时泰州seo网站推广
  • 行政审批局政务服务网站建设情况从事网络营销的公司
  • 努力把网站建设成为发外链平台
  • 分类网站上怎么做锚文本淘宝seo排名优化
  • 没有工信部备案的网站是骗子吗西安百度百科
  • 免费网站下载直播软件免费百度提升优化
  • vs网站开发参考文献网页浏览器
  • 红色旅游网页设计郑州百度快照优化
  • 百度推广怎么做网站seo实战技术培训
  • 徐州网站建设 网站制作seo关键词排名优化怎样收费
  • 搜索引擎如何找到网站友情链接是免费的吗
  • wordpress数据库编码seo优化师是什么
  • 网站建设公司哪家强seo管理是什么
  • 南部网站建设项目外包平台
  • wordpress 文章页面怎样全屏显示南京百度推广优化
  • 提供给他人做视频解析的网站源码在线看网址不收费不登录
  • 安装wordpress报错seo刷排名软件
  • 小型营销企业网站建设策划app推广渠道在哪接的单子