Json入门及API讲解

一、什么是Json。

(1)JSON(Java Script Object Notation(记号,标记))是一种轻量级的数据交换语言,以文本字符串为基础,且易于让人阅读。

(2)JSON采用完全独立于任何程序语言的文本格式,使JSON成为理想的数据交换语言。

二、Json与xml、properties的区别。

(1)Json是轻量级的,而xml是重量级的(web.xml),目前xml一般用于框架的配置。

(2)Json是有结构的,但是properties仅仅是K-V。

三、.Json具有的形式。

(1)对象:

对象(object) 是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。

<script type="text/javascript">
		var value={
			id:1,
			name:"小明",
			age:12
		};
		document.write("id:"+value.id+"<br/>");
		document.write("name:"+value.name+"<br/>");
		document.write("age:"+value.age+"<br/>");

(2)数组:

数组(array) 是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。

{ "name":"网站", "num":3, "sites":[ "Google", "Runoob", "Taobao" ] }

(3)值:

值(value) 可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。

(4)字符串:

字符串(string) 是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。

(5)数值:

数值(number) 也与C或者Java的数值非常相似。只是JSON的数值没有使用八进制与十六进制格式。

四、java与json。

讲述java与json的常用API,包括对象转介绍、拼接json数组、屏蔽指定json报文中的某个节点等操作。(结尾收尾分享代码。)

package org.com.xgs.jsondemo;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;



public class StartDemo {
	Student student =new Student();
	//对象转json
	private void op1(){
		student.setName("小明");
		student.setAge(18);
		student.setSex("男");
		System.out.println(JSONSerializer.toJSON(student));
	}
	private void op2(){
		JSONObject object = new JSONObject();
		object.put("name", "小红");
		object.put("age", 18);
		object.put("sex", "女");
		System.out.println(object);
	}
	private void op3(){		
		JSONObject tcp_cont = new JSONObject();
		tcp_cont.put("req_time", "20170817195159001");
		tcp_cont.put("svc_code", "6010030001");
		tcp_cont.put("api_code", "60100300010001");
		tcp_cont.put("transaction_id", "");
		tcp_cont.put("sign", "");
		tcp_cont.put("version", "1.0");	
		
		JSONObject busi_params = new JSONObject();
		busi_params.put("excess_fee", "106.23");
		
		JSONObject properties = new JSONObject();
		properties.put("event_code", "2101091003");
		properties.put("oper_time", "2017-08-21 19:54:00");
		properties.put("target_obj_type", "1000");
		properties.put("target_obj_id", "804112813903");
		properties.put("accs_nbr", "18919880768");
		properties.put("contact_nbr", "18919880768");
		properties.put("lan_id", "931");
		properties.put("busi_params", busi_params);
				
		JSONObject svc_cont = new JSONObject();
		svc_cont.put("distinct_id", "20170710132134432");
		svc_cont.put("properties", properties);
		
		JSONObject end = new JSONObject();
		end.put("tcp_cont", tcp_cont);
		end.put("svc_cont", svc_cont);
		
		JSONObject contract_root = new JSONObject();
		contract_root.put("contract_root", end);
		System.out.println(contract_root);
	}
	
	//屏蔽age的json节点
	private void op4(){
		JSONObject object = new JSONObject();
		object.put("name", "小红");
		object.put("age", 18);
		object.put("sex", "女");
		JsonConfig config =new JsonConfig();
		config.setExcludes(new String[]{"age"});
		System.out.println(JSONObject.fromObject(object,config));
	}
	//拼接数组
	private void op5(){
		JSONObject object = new JSONObject();
		object.put("name", "小红");
		object.put("age", 18);
		object.put("sex", "女");
		JSONObject object1 = new JSONObject();
		object1.put("name", "小明");
		object1.put("age", 18);
		object1.put("sex", "男");
		JSONArray array =new JSONArray();
		array.add(object);
		array.add(object1);
		JSONObject object3 = new JSONObject();
		object3.put("array", array);
		System.out.println(JSONObject.fromObject(object3));
	}
	public static void main(String[] args) {
		StartDemo demo =new StartDemo();
//		demo.op1();
//		demo.op4();
		demo.op5();
	}
}

五、总结与声明。

本文对json的入门和java json API进行了入门讲解,对新人理解json希望能起到很大的作用,其实http接口用得很广泛,因此拼接报文很常用,最后文章涉及内容如有雷同,纯属个人借鉴和学习,不喜勿喷。

六、代码分享。

java与JSON API代码分享 (本来不想收积分的,但是最低一分。)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值