JSON数据:
{
"name":"张三",
"age":25,
"hobby":
{
"football" : "非常喜欢",
"baskeball": "喜欢",
"calligraphy": "讨厌",
"novel":{
"name1":"小说1",
"name2":"小说2",
"name3":"小说3"
}
}
}
java代码:
package com.jun.main;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import com.jun.utils.ToolUtils;
import net.sf.json.JSONObject;
@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
public class parseJson {
public static void main(String[] args) {
String jsonData = "{\"name\":\"张三\",\"age\":25,\"hobby\":{\"football\" : \"非常喜欢\",\"baskeball\": \"喜欢\",\"calligraphy\": \"讨厌\",\"novel\":{\"name1\":\"小说1\",\"name2\":\"小说2\",\"name3\":\"小说3\"}}}";
System.out.println("name:" + getValueByConfig("name",jsonData));
System.out.println("age:" +getValueByConfig("age",jsonData));
System.out.println("hobby:" +getValueByConfig("hobby",jsonData));
System.out.println("hobby-football:" +getValueByConfig("hobby-football",jsonData));
System.out.println("hobby-baskeball:" +getValueByConfig("hobby-baskeball",jsonData));
System.out.println("hobby-calligraphy:" +getValueByConfig("hobby-calligraphy",jsonData));
System.out.println("hobby-novel:" +getValueByConfig("hobby-novel",jsonData));
System.out.println("hobby-novel-name1:" +getValueByConfig("hobby-novel-name1",jsonData));
System.out.println("hobby-novel-name2:" +getValueByConfig("hobby-novel-name2",jsonData));
System.out.println("hobby-novel-name3:" +getValueByConfig("hobby-novel-name3",jsonData));
System.out.println("\n");
jsonData = replaceConfigValue("name", "李四", jsonData);
jsonData = replaceConfigValue("age", "30", jsonData);
jsonData = replaceConfigValue("hobby-football", "好", jsonData);
jsonData = replaceConfigValue("hobby-baskeball", "很好", jsonData);
jsonData = replaceConfigValue("hobby-calligraphy", "非常好", jsonData);
jsonData = replaceConfigValue("hobby-novel-name1", "小说5", jsonData);
jsonData = replaceConfigValue("hobby-novel-name2", "小说6", jsonData);
jsonData = replaceConfigValue("hobby-novel-name3", "小说7", jsonData);
System.out.println(jsonData);
}
/**
* 获得键对应信息
*/
public static Object getValueByConfig(String key, String jsonData) {
JSONObject dataJson = JSONObject.fromObject(jsonData);
Object value = null;
String[] keyArray = key.split("-");
if (keyArray != null) {
for (int i = 0; i < keyArray.length; i++) {
String pKey = keyArray[i];
if (i == keyArray.length - 1) {
value = dataJson.get(pKey);
} else {
dataJson = dataJson.getJSONObject(pKey);
}
}
}
return value;
}
/**
* 替换键对应的值信息
*/
public static String replaceConfigValue(String key, Object value, String jsonData) {
JSONObject dataJson = JSONObject.fromObject(jsonData);
String[] keyArray = key.split("-");
if (keyArray != null) {
List
jsonList.add(dataJson);
List
List
for (int i = 0; i < keyArray.length; i++) {
String pKey = keyArray[i];
Object pValue = jsonList.get(i).getString(pKey);
if (i == keyArray.length - 1) {
pValue = value;
} else {
JSONObject pJson = JSONObject.fromObject(pValue);
jsonList.add(pJson);
}
keyList.add(pKey);
valueList.add(pValue);
}
for (int i = keyArray.length - 1; i >= 0; i--) {
jsonList.get(i).put(keyList.get(i), valueList.get(i));
if (i > 0) {
valueList.set(i - 1, jsonList.get(i).toString());
}
}
}
return dataJson.toString();
}
}
输出结果:
name:张三
age:25
hobby:{"football":"非常喜欢","baskeball":"喜欢","calligraphy":"讨厌","novel":{"name1":"小说1","name2":"小说2","name3":"小说3"}}
hobby-football:非常喜欢
hobby-baskeball:喜欢
hobby-calligraphy:讨厌
hobby-novel:{"name1":"小说1","name2":"小说2","name3":"小说3"}
hobby-novel-name1:小说1
hobby-novel-name2:小说2
hobby-novel-name3:小说3
{"name":"李四","age":"30","hobby":{"football":"好","baskeball":"很好","calligraphy":"非常好","novel":{"name1":"小说5","name2":"小说6","name3":"小说7"}}}