Java 如何使其添加到现有阵列中
我需要从地图数据动态生成JSON 我需要在下面生成这个JSONJava 如何使其添加到现有阵列中,java,json,Java,Json,我需要从地图数据动态生成JSON 我需要在下面生成这个JSON [ { "name": "Chips & Chocolates", "T2": [ { "name": "Chips & Chocolates***Bummy Chips", "T3": [ { "nam
[
{
"name": "Chips & Chocolates",
"T2": [
{
"name": "Chips & Chocolates***Bummy Chips",
"T3": [
{
"name": "Chips & Chocolates***Bummy Chips***Masala Roasted with peanuts"
},
{
"name": "Chips & Chocolates***Bummy Chips***Nimbu filled"
}
]
}
]
}
]
Chips & Chocolates***Bummy Chips
Chips & Chocolates***Bummy Chips***Masala Roasted with peanuts
Chips & Chocolates***Bummy Chips
Chips & Chocolates***Bummy Chips***Nimbu filled
[
{
"name": "Chips & Chocolates",
"T2": [
{
"name": "Chips & Chocolates***Bummy Chips",
"T3": [
{
"name": "Chips & Chocolates***Bummy Chips***Masala Roasted with peanuts"
}
]
},
{
"name": "Chips & Chocolates***Bummy Chips",
"T3": [
{
"name": "Chips & Chocolates***Bummy Chips***Nimbu filled"
}
]
}
]
}
]
但最终创建了以下JSON
[
{
"name": "Chips & Chocolates",
"T2": [
{
"name": "Chips & Chocolates***Bummy Chips",
"T3": [
{
"name": "Chips & Chocolates***Bummy Chips***Masala Roasted with peanuts"
}
]
},
{
"name": "Chips & Chocolates***Bummy Chips",
"T3": [
{
"name": "Chips & Chocolates***Bummy Chips***Nimbu filled"
}
]
}
]
}
]
这是我的完整程序
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();
int index = data.indexOf(',');
String name = data;
String value = "";
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;
value = fullpath;
System.out.println(fullpath);
json.put("name", fullpath);
JSONArray jsonarray = new JSONArray();
if (remainder.length() > 0) {
jsonarray.put(processString(remainder, level + 1,fullpath));
if(!value.equals(fullpath))
{
json.put("T" + level, jsonarray);
}
}
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("Bummy Chips,Masala Roasted with peanuts(49)");
values.add("Bummy Chips,Nimbu filled(50)");
consilatedMapMap.put("Chips & Chocolates", values);
try {
int level = 2;
JSONArray json = processMap(consilatedMapMap, level);
System.out.println(json);
} catch(JSONException x) {
x.printStackTrace();
System.exit(-1);
}
}
}
我试着把一个条件作为
如果(!value.equals(完整路径))
{
put(“T”+级别,jsonarray);
}我已尝试通过在参数中更改函数
processString()
中的一些代码,而不是传递单个字符串,来获取所需的输出。我已传递了整个列表,并在该函数中处理该列表,如下代码所示:
private static JSONObject processString(List<String> list, int level,String key) throws JSONException {
JSONObject json = new JSONObject();
JSONArray jsonarray = new JSONArray();
String value = "";
String remainder = "";
JSONObject obj = new JSONObject();
for(String data : list)
{
String name = data;
int index = data.indexOf(',');
name = data.substring(0, index);
remainder = data.substring(name.length() + 1);
String fullpath = key+"***"+name;
value = fullpath;
System.out.println(fullpath);
json.put("name", fullpath);
remainder = data.substring(index+1);
int lastindex = remainder.indexOf('(');
if (lastindex > 0) {
remainder = remainder.substring(0,lastindex);
}
String fullpathVal = key+"***"+remainder;
obj.put("name", fullpathVal);
jsonarray.put(obj);
json.put("T" + level, jsonarray);
}
return json;
}
私有静态JSONObject processString(列表、int级别、字符串键)抛出JSONException{
JSONObject json=新的JSONObject();
JSONArray JSONArray=新的JSONArray();
字符串值=”;
字符串余数=”;
JSONObject obj=新的JSONObject();
用于(字符串数据:列表)
{
字符串名称=数据;
int index=data.indexOf(',');
name=data.substring(0,索引);
余数=data.substring(name.length()+1);
字符串完整路径=键+“***”+名称;
值=完整路径;
System.out.println(完整路径);
json.put(“名称”,完整路径);
余数=数据子串(索引+1);
int lastindex=余数.indexOf('(');
如果(lastindex>0){
余数=余数。子字符串(0,lastindex);
}
字符串fullpathVal=key+“***”+余数;
对象put(“名称”,fullpathVal);
jsonarray.put(obj);
put(“T”+级别,jsonarray);
}
返回json;
}
这是基于您的示例数据和输出中所需的内容
这可能会对您有所帮助。您是否尝试使用调试器来跟踪您的问题?请查看您的输出与代码输出不匹配…!!!请记住if(!value.equals(fullpath)),我保留它用于测试。非常感谢Yagnesh,它对我非常有帮助。