如何创建Json数组并向其中添加数据

时间:2014-06-10 19:10:34

标签: java

我正在使用org.json。

这是我生成JSON结构的应用程序

     package com;


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

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

public class Test {
    public static void main(String[] args) throws JSONException {

        Test testJson = new Test();
        Map<String, LinkedList<String>> categoryitemslistMap = new LinkedHashMap<String, LinkedList<String>>();
        JSONObject leafobj = new JSONObject();

        JSONArray T2Array = new JSONArray();
        JSONArray T3Array = new JSONArray();
        JSONArray T4Array = new JSONArray();

        JSONObject T3JsonObj = new JSONObject();
        JSONObject T2JsonObj = new JSONObject();

        LinkedList<String> T3ValuesList = new LinkedList<String>();
        T3ValuesList.add("Bottled");
        T3ValuesList.add("Fountain");

        categoryitemslistMap.put("Soft Drinks", T3ValuesList);

        String t2consildated_Data = "Lemon,Orange";

        for (Map.Entry<String, LinkedList<String>> entry : categoryitemslistMap.entrySet()) {
            String t1data = entry.getKey();
            if (t1data.equalsIgnoreCase("Soft Drinks")) {
                LinkedList<String> t1ChildList = entry.getValue();

                for (String t2Data : t1ChildList) {
                    if (t2consildated_Data != null&& !t2consildated_Data.isEmpty()) {
                        StringTokenizer stt = new StringTokenizer(t2consildated_Data, ",");

                        while (stt.hasMoreTokens()) {
                        String   t3data = stt.nextToken();
                            JSONArray jsonarray = testJson.createLeaf();
                            leafobj.put("leaf", jsonarray);
                            T4Array.put(leafobj);
                        }

                    }
                }

            } // end of processing values of categoryitemslistMap(Linked List)

        } // end of processing categoryitemslistMap

        T3JsonObj.put("T3",T4Array);


        System.out.println(T3JsonObj);
    } // end of main method
    public JSONArray createLeaf() throws JSONException {
        JSONArray ja = new JSONArray();

        for(int i=0;i<2;i++)
        {
            if(i==0)
            {
                JSONObject jo = new JSONObject();
                jo.put("name", "500 ML");
                ja.put(jo);
            }
            else if(i==1)
            {
                JSONObject jo = new JSONObject();
                jo.put("name", "1 Litre");
                ja.put(jo);
            }

        }


        return ja;
    }

}

这样输出就被生成为

{"T3":[{"leaf":[{"name":"500 ML"},{"name":"1 Litre"}]},{"leaf":[{"name":"500 ML"},{"name":"1 Litre"}]},{"leaf":[{"name":"500 ML"},{"name":"1 Litre"}]},{"leaf":[{"name":"500 ML"},{"name":"1 Litre"}]}]}

有人可以告诉我如何生成以下JSON结构?

{
    "Soft Drinks": {
        "T2": [
            {
                "name": "Bottled",
                "T3": [
                    {
                        "name": "Lemon",
                        "leaf": [
                            {
                                "name": "500 ML"
                            }
                        ]
                    },
                    {
                        "name": "Orange",
                        "leaf": [
                            {
                                "name": "500 ML"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "Fountain",
                "T3": [
                    {
                        "name": "Lemon",
                        "leaf": [
                            {
                                "name": "500 ML"
                            }
                        ]
                    },
                    {
                        "name": "Orange",
                        "leaf": [
                            {
                                "name": "500 ML"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

1 个答案:

答案 0 :(得分:0)

来自Javadocs:

enter image description here

所以只需将System.out.println(T3JsonObj);替换为System.out.println(T3JsonObj.toString(4));