JSON 拆分字符串创建?
JSON creation by splitting a string?
所以我遇到这样一种情况,我无法从数据库中获取一些数据 change/update。所以我的 2 列数据是这样的:
例如:
Column1 Column2
Row 1: hello.how.are.you Gracie
Row 2: hello.how.is.she John
Row 3: hello.how.is.he Gurinder
Row 4: hello.from.me Singh
所以我需要创建一个 JSON,它看起来像:
{
"hello":{
"how":{
"are":{
"you":"Gracie"
},
"is":{
"he":"Gurinder",
"she":"John"
}
},
"from":{
"me":"Singh"
}
}
}
我想要一些优化方法来创建我的 JSON。谢谢!
public static void main(String[] args) {
List<String > stringList = new ArrayList();
stringList.add("hello.how.are.you");
stringList.add("hello.how.is.she");
stringList.add("hello.how.is.he");
stringList.add("hello.from.me");
JSONObject response = new JSONObject();
for (String str : stringList) {
String[] keys = str.split("\.");
for (int i = 0; i < keys.length; i++) {
if (response.has(keys[i])) {
} else {
JSONObject jsonObject2 = new JSONObject()
response.append(keys[i], jsonObject2);
}
}
}
}
我正在做这样的事情并试图解决。
您用于输入的内容需要包含所有数据(包括第 2 列)。假设 input
变量是 HashMap<String, String>
等于
{
"hello.how.are.you" : "Gracie",
...
}
目前,您的代码看起来正常。问题是,当您想追加到 JSON 树深处的某个值时,您正在追加到 response
。
JSONObject parent = response;
for(...) {
// If there's no JSON there, just make it
if( !parent.has(keys[i]) ) {
// It's not already in there, so let's make it
parent.put(keys[i], new JSONObject()); // response["hello"] = {}
}
// Now, look at how this works. If keys = ["hello", "how", "are", "you"],
// Then when i == 0, parent <= response["hello"]
// That way you do response["hello"].append("how", {}) on the next iteration
parent = (JSONObject)parent.get(keys[i]);
}
您还需要处理尾部情况,您可以使用类似
的方式来处理
if( i == keys.length - 1 ) {
parent.put(keys[i], input.get(str)); // str = "hello.how.are.you"
// input.get("hello.how.are.you") == "Gracie"
} else ...
所以我用这种方法解决了这个问题:
public static void main(String... r) {
String[] keys = myString.split("//.");
JSONObject target = new JSONObject();
int lastValueIndex = keys.length - 1;
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
if (!target.has(key) && (lastValueIndex == i)) {
target.put(key, myValue);
break;
} else if (!target.has(key)) {
target.put(key, new JSONObject());
}
target = target.getJSONObject(key);
}
}
在使用我的代码之前尝试你自己的方式,谢谢!
所以我遇到这样一种情况,我无法从数据库中获取一些数据 change/update。所以我的 2 列数据是这样的:
例如:
Column1 Column2
Row 1: hello.how.are.you Gracie
Row 2: hello.how.is.she John
Row 3: hello.how.is.he Gurinder
Row 4: hello.from.me Singh
所以我需要创建一个 JSON,它看起来像:
{
"hello":{
"how":{
"are":{
"you":"Gracie"
},
"is":{
"he":"Gurinder",
"she":"John"
}
},
"from":{
"me":"Singh"
}
}
}
我想要一些优化方法来创建我的 JSON。谢谢!
public static void main(String[] args) {
List<String > stringList = new ArrayList();
stringList.add("hello.how.are.you");
stringList.add("hello.how.is.she");
stringList.add("hello.how.is.he");
stringList.add("hello.from.me");
JSONObject response = new JSONObject();
for (String str : stringList) {
String[] keys = str.split("\.");
for (int i = 0; i < keys.length; i++) {
if (response.has(keys[i])) {
} else {
JSONObject jsonObject2 = new JSONObject()
response.append(keys[i], jsonObject2);
}
}
}
}
我正在做这样的事情并试图解决。
您用于输入的内容需要包含所有数据(包括第 2 列)。假设 input
变量是 HashMap<String, String>
等于
{
"hello.how.are.you" : "Gracie",
...
}
目前,您的代码看起来正常。问题是,当您想追加到 JSON 树深处的某个值时,您正在追加到 response
。
JSONObject parent = response;
for(...) {
// If there's no JSON there, just make it
if( !parent.has(keys[i]) ) {
// It's not already in there, so let's make it
parent.put(keys[i], new JSONObject()); // response["hello"] = {}
}
// Now, look at how this works. If keys = ["hello", "how", "are", "you"],
// Then when i == 0, parent <= response["hello"]
// That way you do response["hello"].append("how", {}) on the next iteration
parent = (JSONObject)parent.get(keys[i]);
}
您还需要处理尾部情况,您可以使用类似
的方式来处理if( i == keys.length - 1 ) {
parent.put(keys[i], input.get(str)); // str = "hello.how.are.you"
// input.get("hello.how.are.you") == "Gracie"
} else ...
所以我用这种方法解决了这个问题:
public static void main(String... r) {
String[] keys = myString.split("//.");
JSONObject target = new JSONObject();
int lastValueIndex = keys.length - 1;
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
if (!target.has(key) && (lastValueIndex == i)) {
target.put(key, myValue);
break;
} else if (!target.has(key)) {
target.put(key, new JSONObject());
}
target = target.getJSONObject(key);
}
}
在使用我的代码之前尝试你自己的方式,谢谢!