gpt4 book ai didi

java - 动态生成的 JSON 订单缺失

转载 作者:行者123 更新时间:2023-12-01 12:37:47 25 4
gpt4 key购买 nike

利用 map 数据结构中存在的值,我动态创建一个 JSON。

创建的 JSON 为

{
"item": {
"T1": [
{
"name": "Ice creams",
"T2": [
{
"name": "Ice creams***Stick",
"T3": [
{
"T4": [
{
"name": "Ice creams***Stick***KoolCool***Strawbeerry",
"leaf": [
{
"crust_name": "crust"
}
]
}
],
"name": "Ice creams***Stick***KoolCool"
}
]
}
]
}
]
}
}

问题是 T3 下的名称被附加在其他地方,而不是在它之后,实际上应该是

{
"item": {
"T1": [
{
"name": "Ice creams",
"T2": [
{
"name": "Ice creams***Stick",
"T3": [
{
"name": "Ice creams***Stick***KoolCool",
"T4": [
{
"name": "Ice creams***Stick***KoolCool***Strawbeerry",
"leaf": [
{
"crust_name": "crust"
}
]
}
]
}
]
}
]
}
]
}
}

从功能上讲,名称在属性列表中的第一个或第二个应该没有区别,但是如果不遵循结构,此 JSON 将被传递到前端并且搜索功能将崩溃

有人可以告诉我如何让名字出现在 T3 之后吗?

请看这个 fiddle

http://jsfiddle.net/5wvqkb82/

这是我的 Java 程序。

package com.services;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Test {

private static JSONObject processString(String data, int level,String key) throws JSONException {
JSONObject json = new JSONObject();
JSONArray leafjsonarray = new JSONArray();
int index = data.indexOf(',');
String name = data;
String remainder = "";
if (index < 0) {
index = name.indexOf('(');
if (index > 0) {
name = data.substring(0, index);
}
} else {
name = data.substring(0, index);
remainder = data.substring(name.length() + 1);
}
String fullpath = key+"***"+name;
json.put("name", fullpath);

JSONArray a = new JSONArray();
if (remainder.length() > 0) {
a.put(processString(remainder, level + 1,fullpath));
json.put("T" + level, a);
}

else {


JSONObject leafjsonObj = new JSONObject();
leafjsonObj.put("crust_name", "crust");
leafjsonarray.put(leafjsonObj);
json.put("leaf", leafjsonarray);


}
return json;
}

private static JSONArray processList(List<String> list, int level,String key) throws JSONException {
JSONArray json = new JSONArray();
for (String data : list) {
json.put(processString(data, level,key));
}
return json;
}

private static JSONArray processMap(Map<String, List<String>> map, int level) throws JSONException {

JSONArray array =new JSONArray();
for (String key : map.keySet()) {
JSONObject json = new JSONObject();
json.put("name", key);
json.put("T" + level, processList(map.get(key), level + 1,key));
array.put(json);
}
return array;

}

public static void main(String args[]) {
Map<String, List<String>> consilatedMapMap = new LinkedHashMap<String, List<String>>();

List<String> values = new LinkedList<String>();
values.add("Stick,KoolCool,Strawbeerry(25)");
// values.add("Cone,SSS(25)");

/* List<String> values2 = new LinkedList<String>();
values2.add("Bucket(25)");
*/

//consilatedMapMap.put("Popcorn", values2);
consilatedMapMap.put("Ice creams", values);


try {
int level = 2;
JSONArray json = processMap(consilatedMapMap, level);
JSONObject jsonT1 = new JSONObject();
jsonT1.put("T1",json);
JSONObject sai = new JSONObject();
sai.put("item",jsonT1);

System.out.println(sai);

} catch(JSONException x) {
x.printStackTrace();
System.exit(-1);
}
}
}

最佳答案

您需要查看this 。基本上,您不能依赖对象的顺序。如果您需要它们按顺序排列,您应该使用 JSON 数组。

如果你想使用数组,你的整个排序就会困惑,例如:

 "T4": [
{
"name": "Ice creams***Stick***KoolCool***Strawbeerry",
"leaf": [
{
"crust_name": "crust"
}
]
}
]

将成为

 "T4": [
{
"name": "Ice creams***Stick***KoolCool***Strawbeerry"
},
{
"leaf": [
{
"crust_name": "crust"
}
]
}
]

您已经在程序中使用了 JSON 数组,请使用它并弄清楚您是否真的想继续这种方式。

关于java - 动态生成的 JSON 订单缺失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25418242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com