Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 JsonObject的多个值,没有进入JsonArray的键_Java_Android - Fatal编程技术网

Java JsonObject的多个值,没有进入JsonArray的键

Java JsonObject的多个值,没有进入JsonArray的键,java,android,Java,Android,在安卓系统中,我想让这个结构成为jsonArray: [ {duration: 1, price: 100}, {duration: 2, price: 200}, {duration: 3, price: 300}, {duration: 4, price: 400} ] 我写了这段代码,但它不正确。我如何解决这个问题,以便获得如上所述的输出 JSONArray jsonArray = new JSONArray(); JSONObject json = ne

在安卓系统中,我想让这个结构成为
jsonArray

[
    {duration: 1, price: 100},
    {duration: 2, price: 200},
    {duration: 3, price: 300},
    {duration: 4, price: 400}
]
我写了这段代码,但它不正确。我如何解决这个问题,以便获得如上所述的输出

JSONArray jsonArray = new JSONArray();
JSONObject json = new JSONObject();
JSONObject oneMonth = new JSONObject();

try {
    oneMonth.put("duration", "1");
    oneMonth.put("price", "2");
    json.put("oneMonth", oneMonth);
} catch (JSONException e) {
    e.printStackTrace();
}

JSONObject threeMonth = new JSONObject();
try {
    threeMonth.put("duration", "1");
    threeMonth.put("price", "2");
    json.put("threeMonth", threeMonth);
} catch (JSONException e) {
    e.printStackTrace();
}

JSONObject sixMonth = new JSONObject();
try {
    sixMonth.put("duration", "1");
    sixMonth.put("price", "2");
    json.put("sixMonth", sixMonth);
} catch (JSONException e) {
    e.printStackTrace();
}

JSONObject twelveMonth = new JSONObject();
try {
    twelveMonth.put("duration", "1");
    twelveMonth.put("price", "2");
    json.put("twelveMonth", twelveMonth);
} catch (JSONException e) {
    e.printStackTrace();
}

try {
    jsonArray.put(/*PUT OBJECTS HERE*/);
} catch (JSONException e) {
    e.printStackTrace();
}
尝试以这种方式添加:

JSONArray jsonArray = new JSONArray();

JSONObject oneMonth = new JSONObject();

try {
    oneMonth.put("duration", "1");
    oneMonth.put("price", "100");

} catch (JSONException e) {
    e.printStackTrace();
}

JSONObject threeMonth = new JSONObject();
try {
    threeMonth.put("duration", "2");
    threeMonth.put("price", "200");

} catch (JSONException e) {
    e.printStackTrace();
}

JSONObject sixMonth = new JSONObject();
try {
    sixMonth.put("duration", "3");
    sixMonth.put("price", "300");

} catch (JSONException e) {
    e.printStackTrace();
}

JSONObject twelveMonth = new JSONObject();
try {
    twelveMonth.put("duration", "4");
    twelveMonth.put("price", "400");
      } catch (JSONException e) {
    e.printStackTrace();
}

try {
     jsonArray.put(oneMonth);
     jsonArray.put(threeMonth);
     jsonArray.put(sixMonth);
     jsonArray.put(twelveMonth);
} catch (JSONException e) {
    e.printStackTrace();
}
如果您执行“jsonArray.put(一个月);”会发生什么