Java中解析json字符串

5 篇文章 0 订阅

实际了json的包分好多种,各种导入的jar包不同,它里面包含的函数方法不同;

下面我当前用习惯的就是json-lib-2.4.jar这个jar包,并且引入的函数包是net.sf.json.JSONObject;而不是org.json.JSONObject;

1.获取简单的json字符串中key对应的值

前提是要导入一个json-lib-2.4.jar包

导入这jar包后,类中引用import net.sf.json.JSONObject;这个包时的情况

代码如下:

import net.sf.json.JSONObject;

public class Test {
	public static void main(String[] args) {
		String jsonStr="{\"trans\":\"01\",\"fileId\":\"中心编号\",\"context\":\"文件流的base64字符串\"}";
		
		JSONObject json = JSONObject.fromObject(jsonStr);
		
		String trans=json.getString("trans");
		String fileId=json.getString("fileId");
		String context=json.getString("context");
		
		System.out.println("["+trans+"]["+fileId+"]["+context+"]");
	}

}

效果图:

2.将 JavaBean和Map 解析成 JSON 串, 使用JSONObject 解析:

实体类:

package com.yinxin.server;

public class Person {
	private String id;
	private int age;
	private String  name;
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Person(String id, String name) {
		this.id = id;
		this.name = name;
	}
	public Person(int age, String name) {
		this.age = age;
		this.name = name;
	}
	public Person(String id, int age, String name) {
		this.id = id;
		this.age = age;
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	
	

}

测试类

package com.yinxin.server;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;



public class Test {
	public static void main(String[] args) {
		/**
	        * 解析 HashMap
	        */
	       Map<String, Object> map = new HashMap<String, Object>();
	       map.put("name", "Tom");  //给name赋值
	       map.put("age", 33); //给age赋值
	       //把map转成json格式
	       JSONObject jsonObject = JSONObject.fromObject(map);
	       System.out.println(jsonObject);

	       /**
	        * 解析 JavaBean
	        */
          //将实体类对象转换成json
	       Person person = new Person("A001", "Jack");
	       jsonObject = jsonObject.fromObject(person);
	       System.out.println(jsonObject);

	       /**
	        * 解析嵌套的对象
	        */
          //将对象放进map集合,再将map集合放进json中
	       map.put("person", person);
	       jsonObject = jsonObject.fromObject(map);
	       System.out.println(jsonObject);
	}

}

 测试结果:

3.将json串转换成数组Array

package com.yinxin.server;
import java.util.Arrays;
import net.sf.json.JSONArray;

public class Test {
		public static void main(String[] args) {
			int[] num=new int[]{89,90,99};
		       JSONArray jsonArray = JSONArray.fromObject(num);
		       Object array = JSONArray.toArray(jsonArray);
		       System.out.println(array);
		       System.out.println(Arrays.asList((Object[]) array));
		   }

}

测试结果:

 4.将 Json 串转成 JavaBean/Map

package com.yinxin.server;
import java.util.Map;
import net.sf.json.JSONObject;
public class Test {
		public static void main(String[] args) {
			   /**
		        * 将 Json 形式的字符串转换为 Map
		        */
		       String str = "{\"name\":\"cy\",\"age\":22}";
		       JSONObject jsonObject = JSONObject.fromObject(str);
		       Map<String, Object> map = (Map<String, Object>) JSONObject.toBean(jsonObject, Map.class);
		       System.out.println(map);

		       /**
		        * 将 Json 形式的字符串转换为 JavaBean
		        */
		       str = "{\"id\":\"100101\",\"name\":\"zhangsan\"}";
		       jsonObject = JSONObject.fromObject(str);
		       System.out.println(jsonObject);
		       Person person = (Person) JSONObject.toBean(jsonObject, Person.class);
		       System.out.println(person);
		   }

}

测试结果:

  • 13
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醉梦洛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值