Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 最终创建了错误的JSON_Java_Json - Fatal编程技术网

Java 最终创建了错误的JSON

Java 最终创建了错误的JSON,java,json,Java,Json,我需要生成这个JSON对象 { "Soft Drinks": { "T2": [ { "name": "Bottled", "T3": [ { "name": "Lemon", "T4": [ {

我需要生成这个JSON对象

{
    "Soft Drinks": {
        "T2": [
            {
                "name": "Bottled",
                "T3": [
                    {
                        "name": "Lemon",
                        "T4": [
                            {
                                "leaf": [
                                    {
                                        "name": "500 ML"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
}
但我总是以创造这个JSON而告终

{
    "Soft Drinks": {
        "T2": [
            {
                "name": "Bottled",
                "T3": [
                    {
                        "name": "Lemon",
                        "leaf": [
                            {
                                "name": "500 ML"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}
叶级对象应位于不同的对象下,即T4

这是我的节目

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

public class BuildJSOn {
    public static void main(String[] args) throws JSONException {
        JSONObject leaf = new JSONObject().put("name", "500 ML");

        JSONObject lemon = new JSONObject().put("name", "Lemon").put("leaf", new JSONArray().put(leaf));

        JSONArray t3Array = new JSONArray().put(lemon);


        JSONObject bottled = new JSONObject().put("name", "Bottled").put("T3", t3Array);

        JSONObject softDrink = new JSONObject().put("T2", new JSONArray().put(bottled));
        JSONObject json = new JSONObject().put("Soft Drinks", softDrink);

        System.out.println(json);
    }
}

你好像什么地方都没提到T4。您没有说明您正在使用的库,或者您正在使用的库是如何工作的(如果您不知道如何向其中添加数组,我假设您不是自己编写的),但我想您需要一些与添加早期数组时类似的东西:

JSONArray t3Array = new JSONArray().put(lemon).put("T4", new JSONArray().put(leaf));

这并不是一个很好的解决方案,数据结构看起来有点奇怪,但您需要在某处添加一个
JSONArray
T4

根据您需要的JSON结构,代码应该是这样的

JSONObject lemon = new JSONObject().put("name", "Lemon");
JSONArray t4 = new JSONArray().put(new JSONObject().put("leaf", new JSONArray().put(leaf)));
lemon.put("T4", t4);
试试这个:

 JSONArray t4Array= new JSONArray();
        JSONArray leaf= new JSONArray();
        JSONObject newLeaf = new JSONObject().put("name", "500 ML");
        leaf.put(newLeaf);
        JSONObject t4Obj= new JSONObject().put("leaf",leaf);
        t4Array.put(t4Obj);
        JSONObject lemon = new JSONObject().put("name", "Lemon").put("T4", t4Array);

        JSONArray t3Array = new JSONArray().put(lemon);


        JSONObject bottled = new JSONObject().put("name", "Bottled").put("T3", t3Array);

        JSONObject softDrink = new JSONObject().put("T2", new JSONArray().put(bottled));
        JSONObject json = new JSONObject().put("Soft Drinks", softDrink);

希望有帮助。

我们来看看您的代码。请注意,它在任何地方都没有提到T4。你认为这是从哪里来的?是的,我不知道怎么把T4放在那里,所以我把它忘了。看看你是怎么把“T3”放的。现在真的不清楚这里的问题是什么,因为你已经得到了它的其余部分。。。听起来你想要一个包含“叶子”属性的对象。。。然后你想用一个“T4”属性把这个对象放进柠檬对象中…是的,正确,我需要把叶子放在T4对象下面。那么就这样做吧!创建一个新对象,将“leaf”属性置于其下,然后使用T4属性放置该对象。您了解现有代码是如何工作的吗?如果是这样,我看不出问题出在哪里。(事实上,从外观上看,T4属性应该是一个包含单个对象的数组…)我遇到了一个编译错误,请您提供帮助。你能把整个代码放进去吗?@PreethiJain-编辑了代码片段。我在那里错过了一件小事。