如何将其添加到现有Array中

时间:2014-08-22 05:56:15

标签: java json

我需要从Map Data动态生成JSON

我需要在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***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);
        }
}
}

当我运行它时,会显示以下内容

上述程序的输出

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"
                    }
                ]
            }
        ]
    }
]

我试过把条件设为

如果(!value.equals(FULLPATH))             {                  json.put(&#34; T&#34; + level,jsonarray);             }

1 个答案:

答案 0 :(得分:1)

我已尝试使用您的数据来获取所需的输出,方法是在参数中更改函数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;
    }

这取决于您的样本数据以及您在输出中所需的内容。

愿这对你有所帮助。