メモと備忘録

メモや備忘録を残します

【Java】HTTPリクエストを想定したJSONの更新【改行、ネストに対応】

・概要
ネストされていたり、パラメータ毎に改行されていたり、パラメータ値にリストが使われていたりするJSONのパラメータ値を更新する事が可能です。

・利用想定
HTTP通信時

・内容
JSONparser クラス

public class JSONparser {

	/*
	 * JSONの更新処理
	 * JSON		JSONデータ
	 * param	更新対象のパラメータ名
	 * value	更新後のパラメータ値
	 * return	更新後のJSONデータ
	 * error	nullを返す
	 */
	public String updateJSON(String JSON, String param, String value){
		String resultStr = null;
		int[] index = JSONIndexOf(JSON, param);
		if(index[0] != -1 && index[1] != -1){
			StringBuilder sb = new StringBuilder(JSON);
			sb.replace(index[0], index[1], value);
			resultStr = sb.toString();
		}
		return resultStr;
	}

	/*
	 * JSONのパラメータ値取得処理
	 * JSON		JSONデータ
	 * param	取得対象のパラメータ名
	 * return	文字列
	 * error	nullを返す
	 */
	public String getJSON(String JSON, String param){
		String resultStr = null;
		int[] index = JSONIndexOf(JSON, param);
		if(index[0] != -1 && index[1] != -1){
			resultStr = JSON.substring(index[0], index[1]);
		}
		return resultStr;
	}

	/*
	 * JSONのパラメータ検索処理
	 * return	int[パラメータ値の開始位置, パラメータ値の終了位置]
	 * error	-1を返す
	 */
	public int[] JSONIndexOf(String JSON, String param){
		int[] resultInt = {-1, -1};

		StringBuilder sb = new StringBuilder();
		sb.append(JSON);

		String searchParam = "\"" + param + "\"";
		int paramIndex = JSON.indexOf(searchParam) + searchParam.length();

		int nextQuotationIndex = JSON.indexOf("\"", paramIndex);
		int nextCommmaIndex = JSON.indexOf(",", paramIndex);
		int nextBraceIndex = JSON.indexOf("}", paramIndex);

		// パラメータ値は文字列か
		boolean isString = nextQuotationIndex < nextCommmaIndex && nextCommmaIndex != -1;
		isString = (isString == true) && (nextQuotationIndex < nextBraceIndex && nextBraceIndex != -1);
		isString = isString == true && nextQuotationIndex != -1;

		if (isString) {
			//	文字列のパラメータ値を取得
			++nextQuotationIndex;
			int endQuotationIndex = JSON.indexOf("\"", nextQuotationIndex);
			int escapeIndex = JSON.indexOf("\\\"", nextQuotationIndex);

			while (endQuotationIndex > escapeIndex && escapeIndex != -1) {
				endQuotationIndex = JSON.indexOf("\"", escapeIndex + 2);
				escapeIndex = JSON.indexOf("\\\"", escapeIndex + 2);

			}
			resultInt[0] = nextQuotationIndex;
			resultInt[1] = endQuotationIndex;

		} else {
			int colonIndex = JSON.indexOf(":", paramIndex) + 1;
			String targetValue = "";
			boolean hasNextParam = nextCommmaIndex < nextBraceIndex && nextCommmaIndex != -1;

			// パラメータ値が数値で、次のパラメータが存在する
			if (hasNextParam) {
				targetValue = JSON.substring(colonIndex, nextCommmaIndex);

			}
			// パラメータ値が数値で、次のパラメータが存在しない
			else {
				targetValue = JSON.substring(colonIndex, nextBraceIndex);

			}

			//	パラメータ値のIndexを取得
			String replaced = targetValue.replaceAll("[ || ||\t||\r\n]", "");
			int beginIndex = JSON.indexOf(replaced, colonIndex);
			int endIndex = beginIndex + replaced.length();
			resultInt[0] = beginIndex;
			resultInt[1] = endIndex;

		}
		return resultInt;
	}
}

・使用例
次のようなJSONデータの場合

{
	"Array":[001,002,003],
	"Nest":{
		"name":"john"
		,"gender":"man"
		,"ID":123
	}
}
// JSON		:JSONデータ
// param	:パラメータ名
// value	:パラメータ値
JSONparser jsonParser = new JSONparser();

String param = "ID";
String param = "XXX";
jsonParser.getJSON(JSON, param));
jsonParser.updateJSON(JSON, param, value);

「ID」を取得します。

jsonParser.getJSON(JSON, param));
//  123

「ID」の値を「XXX」へ更新し、JSONを返却します。

jsonParser.updateJSON(JSON, param, value);
//{
//	"Array":[001,002,003],
//	"Nest":{
//		"name":"john"
//		,"gender":"man"
//		,"ID":XXX
//	}
//}