简体   繁体   English

无法建立此复杂的JSON结构

[英]Unable to build this Complex JSON Structure

I have a inter-related Maps representing the below data . 我有一个表示以下数据的相互关联的地图。

{

        "Soft Drinks": {
            "Tin": [
                {
                    "Lean Apple": [
                        {
                            "name": "1 Litre"
                        },
                        {
                            "name": "2 Litre"
                        }
                    ]
                },
                {
                    "Clear": [
                        {
                            "name": "7 Litre"
                        },
                        {
                            "name": "10 Litre"
                        }
                    ]
                }
            ],
            "Bottled": [

            ]
        }
    }

This is my code representing above json data in form of Java code 这是我的代码,以Java代码的形式表示上述json数据

package test; 包装测试;

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

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

public class Post {

    public static void main(String args[]) throws JSONException
    {
        LinkedList<String> forTinItemsList = new LinkedList<String>();
        LinkedList<String> forBottleItemsList = new LinkedList<String>();

        JSONObject jsonobj = new JSONObject(); 

        Map<String,LinkedList<String>> categoryitemsMap = new LinkedHashMap<String,LinkedList<String>>();

        forTinItemsList.add("Lean Apple");
        forTinItemsList.add("Clear");


        forBottleItemsList.add("Lemon");
        forBottleItemsList.add("Clear");



        categoryitemsMap.put("Tin", forTinItemsList);
        categoryitemsMap.put("Bottled", forBottleItemsList);

        // completion of Categories.


        Map<String,LinkedList<String>> subcategory = new LinkedHashMap<String,LinkedList<String>>();

        LinkedList<String> forLemonItems = new LinkedList<String>();
        forLemonItems.add("1 Litre");
        forLemonItems.add("2 Litre");

        subcategory.put("Lemon", forLemonItems);



        LinkedList<String> forClearItems = new LinkedList<String>();
        forClearItems.add("7 Litre");
        forClearItems.add("10 Litre");

        subcategory.put("Clear", forClearItems);


        for (Map.Entry<String, LinkedList<String>> entry : categoryitemsMap.entrySet())
        {
            String key = entry.getKey();

            LinkedList<String> list = entry.getValue();

            for(String value : list)
            {
                System.out.println(key+"\t"+value);
            }

            //jsonobj.put(entry, arg1);

        }


    }

}

Could anybody please tell me how can i build the above JSON Structure ?? 有人可以告诉我如何构建上述JSON结构吗?

I was trying with different things m, but i was unsuccessful , the problem i was facing is that i am getting the Bottled Array is also filling up with the same Tin Array items . 我尝试使用m进行其他操作,但未成功,所面临的问题是我得到的瓶装阵列也充满了相同的罐头阵列项目。

To give you an idea of the unholy abomination things would become if you did it this way: here's the data structure that you would have to create to capture the JSON you propose. 如果您这样做,就可以使您了解邪恶的可憎之处:这是您必须创建的数据结构才能捕获您提出的JSON。

Map<String, Map<String, List<Map<String, List<Map<String, String>>>>>> items;

So, feel free to implement this, but if it was me, I would build a data model and then map it with Jackson. 因此,可以随意实现它,但是如果是我,我将建立一个数据模型,然后将其与Jackson映射。

有图书馆为您做这件事GSONJackson

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM