Java 向json文件中添加无键数组
我必须制作一个JSON格式的文件,该文件必须如下所示: xyz.jsonJava 向json文件中添加无键数组,java,arrays,json,Java,Arrays,Json,我必须制作一个JSON格式的文件,该文件必须如下所示: xyz.json [ { "imagelist": "/oracle/public/oel6", "label": "test_higgs", "shape": "small", "name" : "/Compute-computecli1/computeadmin/", "networking" : { "eth0" : {
[
{
"imagelist": "/oracle/public/oel6",
"label": "test_higgs",
"shape": "small",
"name" : "/Compute-computecli1/computeadmin/",
"networking" : {
"eth0" : {
"ipnetwork" : "/Compute-computecli1/computeadmin/ipnet"
}
}
}
]
数组应该添加到JSON文件中,不带{},这些花括号必须放在JSON数组中
代码
{
instances:[
{
"imagelist": "/oracle/public/oel6",
"label": "test_higgs",
"shape": "small",
"name" : "/Compute-computecli1/computeadmin/",
"networking" : {
"eth0" : {
"ipnetwork" : "/Compute-computecli1/computeadmin/ipnet"
}
}
}
]
}
是:
这段代码将json数组作为值添加到“instance”键,但我想添加不带json键的json数组
JsonObject ipnetwork = new JsonObject();
ipnetwork.addProperty("ipnetwork", ipNetworkName);
JsonObject interface_type = new JsonObject();
interface_type.add("eth0", ipnetwork);
JsonObject instance = new JsonObject();
instance.addProperty(imageListCmdText, "/oracle/public/oel6");
instance.addProperty("label","test_higgs");
instance.addProperty("shape","small");
instance.addProperty("name","/"+customerName);
instance.add("networking",interface_type);
JsonArray instances = new JsonArray();
instances.add(instance);
JsonObject launch_plan = new JsonObject();
launch_plan.add("instances", instances);
请说明如何更改此代码以获得上述要求的输出
JsonObject launch_plan = new JsonObject();
launch_plan.add("instances", instances);
这两行用大括号创建JSON对象。您不需要它们,只需删除它们并使用实例
,它没有大括号,因为它是json数组,而不是json对象
JsonObject ipnetwork = new JsonObject();
ipnetwork.addProperty("ipnetwork", ipNetworkName);
JsonObject interface_type = new JsonObject();
interface_type.add("eth0", ipnetwork);
JsonObject instance = new JsonObject();
instance.addProperty(imageListCmdText, "/oracle/public/oel6");
instance.addProperty("label","test_higgs");
instance.addProperty("shape","small");
instance.addProperty("name","/"+customerName);
instance.add("networking",interface_type);
JsonArray instances = new JsonArray();
instances.add(instance);
// not needed
//JsonObject launch_plan = new JsonObject();
//launch_plan.add("instances", instances);
对到目前为止你都做了些什么?你的代码在哪里?有什么问题?{instance:[{“imagelist”:/oracle/public/oel6”,“label”:“test_higgs”,“shape”:“small”,“name”:/Compute-computecli1/computeadmin/,“networking”:{“eth0”:{“ipnetwork”:“/Compute-computecli1/computeadmin/ipnet”}}}}}我尝试了这个代码!!但我不知道如何获得问题中提到的上述代码问题中没有代码,这只是(问题的一部分)一个JSON。如果你想得到帮助,你需要展示你的代码并问一个你有困难的特定问题。(因为这个问题太宽泛了,不适合SO)在问题中添加了代码,请看一看。OP可能(也可能不)还必须修改代码,以便将创建的JSON数组写入文件(取决于它现在是如何实现的,它没有显示在提供的代码中)@YashaJadwani我包括了它。@我完全同意!在这种情况下,我们需要OP来编辑问题