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

小叮当网站建设网络搭建教程

小叮当网站建设,网络搭建教程,一流的山西网站建设,泉州建设银行网站2019-10-29 18:53:13一丶概览json-simp是一个方便java的json处理,提供了解码或编码json的功能二丶功能. 使用轻量级的库来解码/解析和转换JSON文本. 灵活,简单并且易于被Map和List接口重用;. 支持流式的JSON文本输出;. 高性能&a…

2019-10-29   18:53:13

一丶概览

json-simp是一个方便java的json处理,提供了解码或编码json的功能

二丶功能

. 使用轻量级的库来解码/解析和转换JSON文本

. 灵活,简单并且易于被Map和List接口重用;

. 支持流式的JSON文本输出;

. 高性能;

. 不依赖其它的库;

. 所有的代码和执行文件都和JDK 1.2兼容

.支持json数组的解码和编码

三丶使用

1.创建一个java工程

.在工程中导入json-simple的jar包

7f0342b6278a53e9d1b0843ab587ecf8.png

.右键工程选build path

73964f11e8b5a1afe5e2347f14fba2c8.png

然后点configure....

0ba997c33ed01ec4fd68700bd9f12e4e.png

.或者直接使用maven导入

com.googlecode.json-simple

json-simple

1.1.1

2.解析json数据

. 普通json解析

packagecom.lxl.learn.json;importorg.json.simple.JSONObject;importorg.json.simple.parser.JSONParser;public classjson {public static void main(String[] args) throwsException {

String test_json= "{\"temp\":\"27.9度\",\"city\":\"北京\",\"weather\":\"多云转阴\",\"WS\":\"小于3级\",\"temp2\":\"31℃\",\"WD\":\"南风\",\"temp1\":\"18℃\"}";

JSONObject json= (JSONObject) newJSONParser().parse(test_json);

System.out.println("城市:"+json.get("city").toString());

System.out.println("温度:"+json.get("temp").toString());/*输出

* 城市:北京

* 温度:27.9度*/}

}

. json数组解析

packagecom.lxl.learn.json;importorg.json.simple.JSONArray;importorg.json.simple.JSONObject;importorg.json.simple.parser.JSONParser;public classjson {public static void main(String[] args) throwsException {

String data= ""+

"[\r\n" +

"{\"city\":\"北京\",\"temp\":\"27.9\",\"WD\":\"南风\",\"WS\":\"小于3级\",\"temp1\":\"18℃\",\"temp2\":\"31℃\",\"weather\":\"多云转阴\"},\r\n" +

"{\"city\":\"广州\",\"temp\":\"26.6\",\"WD\":\"东南风\",\"WS\":\"小于3级\",\"temp1\":\"26℃\",\"temp2\":\"29℃\",\"weather\":\"阵雨转暴雨\"},\r\n" +

"{\"city\":\"广元\",\"temp\":\"24.8\",\"WD\":\"北风\",\"WS\":\"小于3级\",\"temp1\":\"16℃\",\"temp2\":\"28℃\",\"weather\":\"阴转多云\"},\r\n" +

"{\"city\":\"达州\",\"temp\":\"20.2\",\"WD\":\"南风\",\"WS\":\"小于3级\",\"temp1\":\"17℃\",\"temp2\":\"27℃\",\"weather\":\"阵雨转多云\"}\r\n" +

"]";//json解析数组

JSONArray dataArr = (JSONArray) newJSONParser().parse(data);//得到数组下标0,这里得到的数据可以直接强转换为JSONObject(特此记一下)

JSONObject firstData = (JSONObject) dataArr.get(0);

System.out.println("城市:"+firstData.get("city").toString());

System.out.println("温度:"+firstData.get("temp").toString());/*输出

* 城市:北京

* 温度:27.9*/}

}

.json的多级解析,(更多级类似)

packagecom.lxl.learn.json;importorg.json.simple.JSONArray;importorg.json.simple.JSONObject;importorg.json.simple.parser.JSONParser;public classjson {public static void main(String[] args) throwsException {

String data= "{\"data\":[\r\n" +

"{\"city\":\"北京\",\"temp\":\"27.9\",\"WD\":\"南风\",\"WS\":\"小于3级\",\"temp1\":\"18℃\",\"temp2\":\"31℃\",\"weather\":\"多云转阴\"},\r\n" +

"{\"city\":\"广州\",\"temp\":\"26.6\",\"WD\":\"东南风\",\"WS\":\"小于3级\",\"temp1\":\"26℃\",\"temp2\":\"29℃\",\"weather\":\"阵雨转暴雨\"}\r\n" +

"]}";//转换json

JSONObject json_data = (JSONObject) newJSONParser().parse(data);//得到data的数组

JSONArray dataArr = (JSONArray) json_data.get("data");//得到数组下标0

JSONObject firstData = (JSONObject) dataArr.get(0);

System.out.println("城市:"+firstData.get("city").toString());

System.out.println("温度:"+firstData.get("temp").toString());/*输出

* 城市:北京

* 温度:27.9*/}

}

3.编码数据

.构造普通json

packagecom.lxl.learn.json;importorg.json.simple.JSONObject;public classjson {public static void main(String[] args) throwsException {

JSONObject json= newJSONObject();

json.put("city", "北京");

json.put("temp", "27.9度");

System.out.println(json.toJSONString());

/*

* 输出: {"temp":"27.9度","city":"北京"}

*

*/

}

}

.构造数组json

packagecom.lxl.learn.json;importorg.json.simple.JSONArray;importorg.json.simple.JSONObject;public classjson {public static void main(String[] args) throwsException {

JSONArray dataArr= newJSONArray();//第一项数据

JSONObject json1 = newJSONObject();

json1.put("city", "北京");

json1.put("temp", "27.9度");

dataArr.add(json1);

JSONObject json2= newJSONObject();

json2.put("city", "广州");

json2.put("temp", "19.5度");

dataArr.add(json2);

System.out.println(dataArr.toJSONString());/** 输出

* [{"temp":"27.9度","city":"北京"},{"temp":"19.5度","city":"广州"}]*/}

}

.使用Map构造json

packagecom.lxl.learn.json;importjava.util.LinkedHashMap;importjava.util.Map;importorg.json.simple.JSONValue;public classjson {public static void main(String[] args) throwsException {

Map json1= newLinkedHashMap();

json1.put("city", "北京");

json1.put("temp", "27.9度");

json1.put("temp1", "13度");

json1.put("temp2", "30度");

String data= newJSONValue().toJSONString(json1);

System.out.println(data);/** 输出

* {"city":"北京","temp":"27.9度","temp1":"13","temp2":"30度"}*/}

}

.使用List构造json数组

packagecom.lxl.learn.json;importjava.util.LinkedList;importjava.util.List;importorg.json.simple.JSONObject;importorg.json.simple.JSONValue;public classjson {public static void main(String[] args) throwsException {//这里使用map构造普通json也一样

JSONObject obj1= newJSONObject();

obj1.put("city", "北京");

obj1.put("temp", "29.7");

JSONObject obj2= newJSONObject();

obj2.put("city", "广州");

obj2.put("temp", "19");

List li= newLinkedList();

li.add(obj1.toJSONString());

li.add(obj2.toJSONString());

String json= newJSONValue().toJSONString(li);

System.out.println(json);/** 输出

* ["{\"temp\":\"29.7\",\"city\":\"北京\"}","{\"temp\":\"19\",\"city\":\"广州\"}"]*/}

}

四丶其他

只是对json的简单使用,里面还有合并json数组或者json。分别调用putall方法即可

http://www.lbrq.cn/news/2376865.html

相关文章:

  • 网页制作下载安装包无锡seo公司哪家好
  • 柳江网站建设网址导航下载到桌面
  • 免费html网页模板网站太原百度快速优化
  • 什么博客可以做网站公司调查公司
  • 网站设计师培训中心关键词百度指数查询
  • php网站后台建设网站维护需要学什么
  • 湖北建设委员会网站外链推广软件
  • 网站建设知名公司排名网站seo排名公司
  • 衡阳网站建设开发价格手机百度收录提交入口
  • 网站浏览记录怎么做快速建站
  • 机构组织网站建设推广下载
  • 营销网站建设规划概念html期末大作业个人网站制作
  • 怎样修改网站标题如何做好一个网站
  • 简述网站的制作步骤品牌策划包括哪几个方面
  • jsp网站开发需要什么技术免费友链平台
  • 微信商城网站如何做网络服务平台
  • 专注于上海seo做网站建设网站关键词如何快速上首页
  • 委托他人做公司网站的税率百度竞价排名的利与弊
  • 潍坊 seo网站建设济南seo全网营销
  • 不知道是谁做的网站 输入学号新闻式软文范例
  • 网页制作建立站点福清seo
  • 网页设计与制作例子影响关键词优化的因素
  • 为wordpress首页添加关键词seo营销课程培训
  • wordpress分类模板下载seo网站优化方案摘要
  • 建设企业网站管理的重要性怎样做百度推广
  • 建立网站定制中国新闻
  • 做网站简单还是做app简单王通seo
  • 傻瓜网站开发软件什么平台可以免费推广产品
  • 零代码平台快排seo排名软件
  • 公司网站建设要注意什么深圳推广公司排行榜
  • OpenTelemetry学习笔记(四):OpenTelemetry 语义约定,即字段映射(1)
  • [Linux]如何設置靜態IP位址?
  • .NET Core EFCore零基础快速入门简单使用
  • LVS实验
  • Python元组(Tuple)指南
  • 深入理解进程等待:wait的简化与waitpid的灵活性