2016-11-30 48 views
-1

我必须做出一个JSON格式的文件,它必须看起来像以下:添加数组JSON文件,没有钥匙

xyz.json

[ 
    { 
     "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数组作为值“实例”键,但我想补充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); 

请告诉我们如何改变这段代码以获得上面提到的输出。

+0

是吗?到目前为止,你尝试过什么?你的代码在哪里?你有什么问题? – UnholySheep

+0

{ 实例:[ { “图像列表”: “/ ORACLE /公共/ oel6”, “标签”: “test_higgs”, “形状”: “小”, “名称”:“/计算-computecli1/computeadmin /”, “网络”:{ “eth0的”:{ “ipnetwork”: “/计算-computecli1/computeadmin/IPNET” } } } ] } –

+0

我试图代码此!!但我没有得到如何获得问题中提到的上述代码 –

回答

1
JsonObject launch_plan = new JsonObject(); 
launch_plan.add("instances", instances); 

这两行创建带花括号的JSON对象。你不需要它们,你可以删除它们并使用instances,它没有花括号,因为它是一个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); 
+0

可以请你分享整个编辑的代码 –

+0

OP可能(或不可能)也必须修改用于将创建的JSON数组写入文件的代码(取决于它现在如何实现,它没有显示在提供的代码中) – UnholySheep

+0

@YashaJadwani我包括它。 – BackSlash