JSON解析和生成(一)

1.JSON是什么

  • 与开发语言无关的,轻量级的数据交换格式。全称是Java Script Object Notation;
  • <K,V>存储模式,是一种标准的文件格式;
  • 它使用文本将数据对象与数组数据类型通信,这种表示法使应用程序很容易解析和生成文件;

标准的Json数据表示:

  • 数据结构:Object;Array;
  • 基本类型:string,number,true,false,null

数据结构-Object:使用花括号{}包含的键值对结构,Key必须是string类型,value为任何类型或数据结构
数据结构-Array:使用中括号[]来起始,并用逗号,来分隔元素

标注:JSON没有日期格式;   标准的JSON不支持任何形式的注释

关于小明主要有下面六个属性,可以通过JSON来表达:

{
	"name":"小明",
	"age":25 ,
	"birthday":"2019-01-01", 
	"hooby":["游泳","画画"],
	"house":null,
	"has_girlfriend":true  
}

2.构建JSON对象的三种方法

还是以上面的“小明为例”,首先创建一个maven项目,结构如下,在pom.xml中引入依赖:

  <dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20160810</version>
  </dependency>

a.使用JSONObject构建json对象

private static void JSONTest() {
		//JSONObject对象
		JSONObject person=new JSONObject();
		Object null1=null;
		try {
			person.put("name", "小明");
			person.put("age", 12);
			person.put("birthday", "2019-01-01");
			person.put("hooby",new String[] {"游泳","画画"} );
			person.put("has_girlfriend", true);
			//null数据不输出,且不能直接赋值
			person.put("house", null1);
			System.out.println(person.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

b.使用Map构建json对象

private static void JsonMapTest() {
		//创建一个Map对象
		Map<String, Object> person=new HashMap<String, Object>();
		Object null1=null;
		person.put("name", "小明");
		person.put("age", 12);
		person.put("birthday", "2019-01-01");
		person.put("hooby",new String[] {"游泳","画画"} );
		person.put("has_girlfriend", true);
		person.put("house", null1);
		System.out.println(new JSONObject(person).toString());
	}

c.使用JavaBean构建json对象

创建一个JavaBean类:

public class Wywbean {

	private String name;
	private int age;
	private String birthday;
	private String[] hobby;
	private boolean has_girlfriend;
	private Object house;
	
...
getter/setter 方法
重写toString()
}
private static void JsonBeanTest() {
		Wywbean person=new Wywbean();
		person.setName("小明");
		person.setAge(12);
		person.setBirthday("2019-01-01");
		person.setHobby(new String[] {"画画","游泳"});
		person.setHas_girlfriend(true);
		person.setHouse(null);
		System.out.println(new JSONObject(person));
	}

3.从文件中读取JSON

为了方便读取,引入依赖:

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.4</version>
 </dependency>

文件xiaoming.json:

{
	"name":"小明",
	"age":25 ,
	"birthday":"2019-01-01", 
	"hooby":["游泳","画画"],
	"house":null,
	"has_girlfriend":true  
}

使用JSONObject读取,注意解析数组时的方法:

public class ReadJSONSample {
	public static void main(String[] args) throws IOException {
		File file=
				new File(ReadJSONSample.class.getResource("/xiaoming.json").getFile());
		String content=FileUtils.readFileToString(file);
		JSONObject object=new JSONObject(content);
		
		System.out.println("姓名是:"+object.getString("name"));
		System.out.println("年龄是:"+object.getInt("age"));
		System.out.println("生日是:"+object.getString("birthday"));
		System.out.println("有女朋友吗?"+object.getBoolean("has_girlfriend"));
		
		//解析一个数组
		JSONArray jsonArray=object.getJSONArray("hooby");
		
		for (int i = 0; i < jsonArray.length(); i++) {
			String string=(String) jsonArray.get(i);
			System.out.println("爱好--"+string);
		}
	}
}

4.结束...

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值