Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.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 android中的Json数组操作_Java_Android_Arrays - Fatal编程技术网

Java android中的Json数组操作

Java android中的Json数组操作,java,android,arrays,Java,Android,Arrays,如果我有一个Json数组对象,如下所示: [ {"values here"}, {"values here"}, {"values here"} ] { "key1":"value1","key2":"value2","key3":"value3" :[ {"values here"}, {"values here"}, {"values here"}

如果我有一个Json数组对象,如下所示:

[
    {"values here"},
    {"values here"},
    {"values here"}
]
    {
        "key1":"value1","key2":"value2","key3":"value3"
         :[
             {"values here"},
             {"values here"},
             {"values here"}
          ]
    }
我想要一个Json数组对象,如下所示:

[
    {"values here"},
    {"values here"},
    {"values here"}
]
    {
        "key1":"value1","key2":"value2","key3":"value3"
         :[
             {"values here"},
             {"values here"},
             {"values here"}
          ]
    }

我是否可以在数组中所有元素的前面插入另一个json对象并将其封闭。

我不确定“封闭”是什么意思

可以将数组的第一个元素作为对象

[
   {
     "key1": "value1",
     "key2": "value2",
     "key3": "value3"
   },
   "values here",
   "values here",
   "values here"
]   
您还可以有一个对象,如

{
   "first": {
     "key1": "value1",
     "key2": "value2",
     "key3": "value3"
   },
   "rest": [
     "values here",
     "values here",
     "values here"
   ]
}

使用下面的例子

JSONObject student1 = new JSONObject();
try {
  student1.put("id", "3");
  student1.put("name", "NAME OF STUDENT");
  student1.put("year", "3rd");
  student1.put("curriculum", "Arts");
  student1.put("birthday", "5/5/1993");

} catch (JSONException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

JSONObject student2 = new JSONObject();
try {
  student2.put("id", "2");
  student2.put("name", "NAME OF STUDENT2");
  student2.put("year", "4rd");
  student2.put("curriculum", "scicence");
  student2.put("birthday", "5/5/1993");

} catch (JSONException e) {
// TODO Auto-generated catch block
  e.printStackTrace();
}


JSONArray jsonArray = new JSONArray();

jsonArray.put(student1);
jsonArray.put(student2);

JSONObject studentsObj = new JSONObject();
studentsObj.put("Students", jsonArray);



String jsonStr = studentsObj.toString();

System.out.println("jsonString: "+jsonStr);

上面的例子摘自

Go this[Nice Qusetion][1],你会得到答案的。我希望[1]:我希望对象:“key1”:“value1”,“key2”:“value2”,“key3”:“value3”是第一个元素,然后是其中的所有元素。如果它在对象的“内部”,那么对象需要有一个
“key4”
,包含数组和其余值。如果对象只有三个关键点,则其中不能有任何其他值。