ラクダにキーJSONのすべての(最初の文字は大文字)

それを自分で行う - JSON、アンダースコアの形でJSONデータを処理する最近のデータは、長い時間を探しているプロセスはキャメルコードを強調できるようにするには見つけられませんでした!

機能:例えば "the_red_apple": "very_nice" "TheRedApple" へ:この "very_nice"。

コード:https://github.com/SkyingzZ/camel_json_keys


//下划线写法转为驼峰写法	like "sample_test_name_balabala" to "SampleTestNameBalabala"
func CamelName(name string) string {
	name = strings.Replace(name, "_", " ", -1)
	name = strings.Title(name)
	return strings.Replace(name, " ", "", -1)
}

func CamelJsonKey(json_data []byte) []byte{
	str := string(json_data)

	var is_quot_first bool = true		//在双引号中为 false  	|	the value in the double quotes is false 
	var first_index int = 0				//左引号的索引			|	the left quote index
	var second_index int = 0			//右引号的索引			|	the right quote index

	var res_str string
	var the_key_index int
	for i, value := range str{
		if !is_quot_first && str[i] == '"'{		//右引号		|	if meet the right quote
			second_index = i
			is_quot_first = !is_quot_first
			
		}else if is_quot_first && str[i] == '"'{	//左引号	|	if meet the left quote
			first_index = i
			the_key_index = len(res_str)
			is_quot_first= !is_quot_first
		}else if is_quot_first && str[i] == ':'{	
			tmp_str := camelName(str[first_index+1: second_index])
			res_str = res_str[:the_key_index]+"\""+tmp_str+"\""
		}else{}

		res_str += string(value)
	}

	return []byte(res_str)
}

効果:

元==>その後

  

公開された22元の記事 ウォン称賛18 ビュー20000 +

おすすめ

転載: blog.csdn.net/Skying_/article/details/103981211