分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

json三-------com.alibaba.fastjson

发布时间:2023-09-06 01:14责任编辑:彭小芳关键词:jsjson

1.需要阿里巴巴的fastjson.jar

2.将json字符串转为JSONObject,通过JSONObject.parseObject(json字符串),取值的话通过json对象的getString(),getIntValue()等等获取JSONObject的值

String student = "{‘name‘:‘张三‘,‘age‘:30}" ;JSONObject json = JSONObject.parseObject(student) ;System.out.println("----------"+json.toString());System.out.println("----------"+json.getString("name"));System.out.println("----------"+json.getIntValue("age"));结果:----------{"age":30,"name":"张三"}----------张三----------30

2.将json字符串转为JSONArray,通过JSONArray.parseArray(json字符串),取值通过循环读取,读取的每一条数据,对象是一个JSONObject,集合就是JSONArray,然后通过json对象的getString(),getIntValue()等等获取JSONObject的值

String stu = "[{‘name‘:‘张三‘,‘age‘:20},{‘name‘:‘李四‘,‘age‘:30}]" ;JSONArray jsonArray = JSONArray.parseArray(stu) ;for (Object o : jsonArray) { ???JSONObject j = JSONObject.parseObject(o.toString()) ; ???System.out.println("-----------"+j.getString("name")); ???System.out.println("-----------"+j.getIntValue("age"));}结果: ???????-----------张三 ???????-----------20 ???????-----------李四 ???????-----------30 ???

3.将对象、集合转为json字符串用JSON.toJSONString() ;

Student s = new Student() ;s.setAge(20);s.setName("张三");String sutdent = JSON.toJSONString(s) ;System.out.println("-----------"+sutdent); ????????List<Student> students = new ArrayList<Student>() ;students.add(s) ;String j = JSON.toJSONString(students) ;System.out.println("--------------"+j);结果:-----------{"age":20,"name":"张三"}--------------[{"age":20,"name":"张三"}]

json三-------com.alibaba.fastjson

原文地址:http://www.cnblogs.com/-scl/p/7601377.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved