java json对象和json字符串互转的方法_将json字符串转换成对象

java json对象和json字符串互转的方法_将json字符串转换成对象包名:importnet.sf.json.JSONObject;1.maven地址<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

包名:import net.sf.json.JSONObject;

1.maven地址

<dependency>    
		<groupId>net.sf.json-lib</groupId>    
		<artifactId>json-lib</artifactId>    
		<version>2.4</version>    
		<classifier>jdk15</classifier>    
</dependency>

2.People对象详情如下:

package pojo;
public class People {


	private int id;
	private int age;
	private String name;
	
	
	public People(int id, int age, String name) {
		super();
		this.id = id;
		this.age = age;
		this.name = name;
	}
	
	public People() {
		super();
	}


	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public String getName() {
		return name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", age=" + age + ", name=" + name + "]";
	}
	
}

 3.json字符串转为JSONObject对象:

String jsonStr = "{\"name\":\"1\",\"age\":1,\"id\":0}";
// json字符串转为JSONObject 对象
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
System.out.println("name:" + jsonObject.get("name"));
System.out.println("age:" + jsonObject.get("age"));
System.out.println("id:" + jsonObject.get("id"));

4.JSONObject转为json字符串:

// JSONObject转为json字符串
String string = jsonObject.toString();
System.out.println("string" + string);

 

 

5.JSONObject对象转化为javaBean对象:

// JSONObject转为javaBean
People people = (People)JSONObject.toBean(jsonObject, People.class);
System.out.println(people);

6.有时候,我们需要将一个集合json转化为一个java的List<T>集合:

例如如下的一个json字符串:

[{"age":1,"id":1,"name":"first"},
{"age":2,"id":2,"name":"second"},
{"age":3,"id":3,"name":"third"}]

这个json字符串中包含的数据可以转化为一个List<People>集合,代码如下:

// 将List的Json字符串转化为List<T>集合
		String listStr = "[{\"age\":1,\"id\":1,\"name\":\"first\"},{\"age\":2,\"id\":2,\"name\":\"second\"},{\"age\":3,\"id\":3,\"name\":\"third\"}]";
		JSONArray jsonArray2 = JSONArray.fromObject(listStr);
		List<People> peopleList2 = (List<People>)JSONArray.toCollection(jsonArray2, People.class);
		for(People peo : peopleList2)
		{
			System.out.println(peo);
		}

7.JSONArray的遍历:

List<People> peopleList = new ArrayList<People>();
		peopleList.add(new People(1,1,"first"));
		peopleList.add(new People(2,2,"second"));
		peopleList.add(new People(3,3,"third"));
		JSONArray jsonArray = JSONArray.fromObject(peopleList);
		// JSONArray的遍历
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject2 = jsonArray.getJSONObject(i);
			People people1 = (People)jsonObject.toBean(jsonObject2, People.class);
			System.out.println(people1);
		}

 

 

 

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/190584.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js原生实现FastClick事件

    js原生实现FastClick事件js原生实现FastClick事件注明:本人学习javascript时间不长,最近一直在做web端的手机网页和微信应用,由于最近有用到类似fastclick的功能,在原来的程序中用touchstart和touchend事件模拟,现在尝试将其封装,得到了以下两种有问题的方案。分享给大家,另求大神指导

    2022年6月19日
    27
  • istio解决了什么问题(istio k8s)

    问题简述通过istio实现灰度发布,浏览器访问报404错误,但是通过curl传递一个Host请求头就能访问成功。问题复现RancherUI界面启动Istio,并开启ingress网关命名空间启动Istio自动注入部署nginx应用###deploy-nginx-v1.yamlapiVersion:apps/v1kind:Deploymentmetadata:labels:app:nginxversion:v1name:nginx-v1n

    2022年4月16日
    113
  • 增量表全量表拉链表区别_hive 增量数据更新

    增量表全量表拉链表区别_hive 增量数据更新一、概念增量表:记录更新周期内新增的数据,即在原表中数据的基础上新增本周期内产生的新数据;全量表:记录更新周期内的全量数据,无论数据是否有变化都需要记录;拉链表:一种数据存储和处理的技术方式,可以记录数据的历史信息,记录数据从开始一直到当前所有变化的信息。二、举例详解增量表:以页面访问数据表为例,假设该表从2020-06-01开始记录数据,按天更新,分区为dt。2020-06-01产生了三条访问数据,如下表:2020-06-02首页和商详页又产生了2条访问数据,该两条即为2020-06-

    2022年10月17日
    3
  • Activity 跳转的生命周期变化

    Activity 跳转的生命周期变化####(1)Activity1跳转到Activity2的生命周期流程1.Activity1启动:Activity1:onCreate()Activity1:onStart()Activity1:onResume()2.点击按钮跳转到Activity2:Activity1:onPause()Activity2:…

    2022年5月21日
    55
  • 10家值得关注的新加坡和印度大数据初创公司

    10家值得关注的新加坡和印度大数据初创公司

    2022年1月22日
    73
  • ENVI5.3.1使用Landsat 8影像进行图像融合「建议收藏」

    ENVI5.3.1使用Landsat 8影像进行图像融合「建议收藏」ENVI处理郑州地区遥感图像融合,提高空间分辨率

    2022年7月23日
    45

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号