如何使其添加到现有阵列 [英] How to make it add it to the existing Array

查看:92
本文介绍了如何使其添加到现有阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据地图数据动态生成JSON

I need to produce the JSON dynamically from a Map Data

我需要在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

But ending up creating the following 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);
        }
}
}

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

When i run it , the following gets displayed

上述程序的输出

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

我尝试通过将条件设为

if(!value.equals(fullpath)) { json.put("T" +等级,jsonarray); }

if(!value.equals(fullpath)) { json.put("T" + level, jsonarray); }

推荐答案

我尝试通过更改函数processString()中的某些参数的参数来获取所需的输出,而不是传递单个字符串,而是通过整个列表处理该函数中的列表,如以下代码所示:

I have tried with your data to get your required output by changing some code in function processString() in argument instead of passing single string I have passed whole list and process that list in that function as show in below code :

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;
    }

这是基于您的样本数据以及您在输出中所需要的.

This is based on your sample data and what you required in output.

这对您有帮助吗?

这篇关于如何使其添加到现有阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆