JSON数据格式操作(Java语言)

基础介绍

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成。它基于ECMA262语言规范(1999-12第三版)中JavaScript编程语言的一个子集。 JSON采用与编程语言无关的文本格式,但是也使用了类C语言(包括C, C++, C#, Java, JavaScript, Perl, Python等)的习惯,这些特性使JSON成为理想的数据交换格式。
JSON的结构基于下面两点
1. "名称/值"对的集合 不同语言中,它被理解为对象(object),记录(record),结构(struct),字典(dictionary),哈希表(hash table),键列表(keyed list)
等,
2. 值的有序列表 多数语言中被理解为数组(array)

JSON语法规则

对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。
 数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。
值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。
字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。与C或者Java的字符串非常相似。

JSON示例

JSONObject对象:
{"user":{"name":"hoking","mobileNumber":"137****8888","address":"Zhengzhou"}}
JSONArray对象:
{"users":[{"name":"hoking","mobileNumber":"137****8888","address":"Zhengzhou"},{"name":"hoking","mobileNumber":"137****8888","address":"Zhengzhou"},{"name":"hoking","mobileNumber":"137****8888","address":"Zhengzhou"}]}

演示程序

import java.io.IOException;
import java.util.Iterator;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.app.json.domain.User;

/**	
 * 此工具类需要json.jar 支持
 * @author Mahc
 */
public class JsonUtil {

	private static String jsonObjectStr = "{\"user\":{\"name\":\"hoking\",\"mobileNumber\":\"137****8888\",\"address\":\"Zhengzhou\"}}";
	private static String jsonArrayStr = "{\"users\":[" +
			"{\"name\":\"hoking\",\"mobileNumber\":\"137****8888\",\"address\":\"Zhengzhou\"}," +
			"{\"name\":\"hoking\",\"mobileNumber\":\"137****8888\",\"address\":\"Zhengzhou\"}," +
			"{\"name\":\"hoking\",\"mobileNumber\":\"137****8888\",\"address\":\"Zhengzhou\"}]}";
	
	/**
	 * 生成jsonObjectStr(JSONObject字符串)
	 * @throws JSONException
	 */
	public static void generatesJsonObjectStr() throws JSONException{
		
		JSONObject tagJsonObject = new JSONObject();
		JSONObject contentJsonObject = new JSONObject();
		contentJsonObject.put("name", "hoking");
		contentJsonObject.put("mobileNumber", "137****8888");
		contentJsonObject.put("address", "Zhengzhou");
		tagJsonObject.put("user", contentJsonObject);
		System.out.println(tagJsonObject);
	}
	
	/**
	 * 生成jsonArrayStr(JSONArray字符串)
	 * @throws JSONException
	 */
	public static void generatesJsonArrayStr() throws JSONException{
		
		JSONObject tagJsonObject = new JSONObject();
		JSONArray contentJsonArray = new JSONArray();
		for(int i=0;i<3;i++){
			JSONObject contentItemJsonObject = new JSONObject();
			contentItemJsonObject.put("name", "hoking");
			contentItemJsonObject.put("mobileNumber", "137****8888");
			contentItemJsonObject.put("address", "Zhengzhou");
			contentJsonArray.put(contentItemJsonObject);
		}
		tagJsonObject.put("users", contentJsonArray);
		System.out.println(tagJsonObject);
	}
	
	/**
	 * 解析jsonArrayStr(JSONArray字符串)
	 * @throws JSONException
	 */
	public static void analyzeJsonObjectStr() throws JSONException{
		JSONObject jsonObjectString = new JSONObject(jsonObjectStr);
		JSONObject contentJsonObject = new JSONObject();
		contentJsonObject = jsonObjectString.getJSONObject("user");
		Iterator iterator = contentJsonObject.keys();
		while (iterator.hasNext()) {
			String key = (String)iterator.next();
			String param = contentJsonObject.getString(key);
			System.out.println(key+":"+param);
		}
		System.out.println();
	}
	
	/**
	 * 解析jsonArrayStr(JSONArray字符串)
	 * @throws JSONException
	 */
	public static void analyzeJsonArrayStr() throws JSONException{
		JSONObject jsonObjectString = new JSONObject(jsonArrayStr);
		JSONArray contentJsonArray = new JSONArray();
		contentJsonArray = jsonObjectString.getJSONArray("users");
		for(int i=0;i<contentJsonArray.length();i++){
			JSONObject contentItemJsonObject = contentJsonArray.getJSONObject(i);
			Iterator iterator = contentItemJsonObject.keys();
			while (iterator.hasNext()) {
				String key = (String)iterator.next();
				String param = contentItemJsonObject.getString(key);
				System.out.println(key+":"+param);
			}
			System.out.println();
		}
	}
	
	/**
	 * 将User对象的全部属性与数值以Json字符串格式显示
	 * 重载此方法为object2JsonObjectString(Object obj) 可以实现对任意Bean对象的解析
	 * 此方法需要jackson-all-1.9.0.jar 支持
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static void object2JsonObjectString() throws JsonGenerationException, JsonMappingException, IOException{
		String jsonString = "";
		User user = new User();
		user.setName("Hoking");
		user.setMobileNumber("137****8888");
		user.setAddress("Zhengzhou");
		ObjectMapper objectMapper = new ObjectMapper();
		jsonString = objectMapper.writeValueAsString(user);
		System.out.println(jsonString);
	}
}

需要辅助支持的Jar文件
json.jar、jackson-all-1.9.0.jar。点击下载!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Hoking

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

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

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

打赏作者

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

抵扣说明:

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

余额充值