我有以下字符串。我想从这个字符串关键字获取jsonarray,如id,title等。
Json_obj = new JSONObject(result);
vid = Json_obj.getJSONArray("id");
//vcatid= Json_obj.getJSONArray("title");
vtitle= Json_obj.getJSONArray("title");
vdesc= Json_obj.getJSONArray("content");
//vimgurl= Json_obj.getJSONArray("vimgurl");
vidlike= Json_obj.getJSONArray("likes");
vidview= Json_obj.getJSONArray("views");
//vidfblike= Json_obj.getJSONArray("vidfblike");
//vidtwlike= Json_obj.getJSONArray("vidtwlike");
storagepath= Json_obj.getJSONArray("url");
Log.i("json_Video_id vid.length() ",
"%%%%%%%%%%%%%%%%%%%%%% "
+ vid.length());
[{"id":"39","title":"\u0627\u0644\u0631\u0651\u0628\u0627 ","content":"\r\n\u0627\u0639\u0644\u0627\u0646 \u0641\u064a \u0627\u0644\u062a\u062d\u0630\u064a\u0631 \u0645\u0646 \u0645\u0634\u0643\u0644\u0627\u062a \u0627\u0644\u0631\u0628\u0627 \u0639\u0644\u0649 \u0627\u0644\u0639\u0627\u0644\u0645 \u0623\u062c\u0645\u0639.\r\n\u0641\u064a\u062f\u064a\u0648 \u0643\u0644\u064a\u0628 \u0628\u062f\u0642\u0629 \u0639\u0627\u0644\u064a\u0629 HD\r\n\u0645\u0646\u062a\u062c \u0645\u0646\u0641\u0630 \u0648\u0644\u064a\u062f \u0627\u0644\u0633\u0646\u062f New View","url":"http:\/\/www.youtube.com\/embed\/L9aPhl6eST0","views":"264","likes":"6"},{"id":"44","title":"\u062e\u064a\u0648\u0637 \u0627\u0644\u0646\u0648\u0631 \u0645\u062d\u0645\u062f \u0627\u0644\u0639\u0632\u0627\u0648\u064a ","content":"\u0641\u064a\u062f\u064a\u0648 \u0643\u0644\u064a\u0628 \u0644\u0635\u0627\u0644\u062d \u0645\u0634\u0631\u0648\u0639 \u0627\u0644\u0639\u0634\u0631 \u0627\u0644\u0623\u062e\u064a\u0631\r\n\u0643\u0644\u0645\u0627\u062a \u0633\u0644\u0637\u0627\u0646 \u0627\u0644\u0633\u0628\u0647\u0627\u0646\r\n\u0623\u062f\u0627\u0621 \u0648\u0644\u062d\u0646 \u0645\u062d\u0645\u062f \u0627\u0644\u0639\u0632\u0627\u0648\u064a\r\n\u0627\u062e\u0631\u0627\u062c \u0645\u062d\u0645\u062f \u062e\u0627\u0637\u0631\r\n","url":"http:\/\/www.youtube.com\/embed\/sOoP6SW-wuo","views":"1156","likes":"10"},{"id":"47","title":"\u062a\u0643\u0627\u0641\u0644 \u0646\u0627\u0635\u0631 \u0627\u0644\u0633\u0639\u064a\u062f ","content":"\r\n\u0644\u0644\u0641\u0642\u062f\u0650 \u0623\u0648\u062c\u0627\u0639\u064c \u062a\u062e\u062a\u0628\u0626 \u0628\u064a\u0646 \u062c\u062f\u0631\u0627\u0646 \u0627\u0644\u0623\u0644\u0645 \u0648 \u0623\u0633\u062a\u0627\u0631\u0650 \u0627\u0644\u0635\u0645\u062a .. \u0648 \u062f\u0645\u0648\u0639 \u062a\u0628\u0644\u0650\u0651\u0644\u064f \u0635\u062f\u0631\u0627\u064b \u064a\u0636\u062c\u064f\u0651 \u0628\u0627\u0644\u062d\u0646\u064a\u0646 ..\r\n\r\n\u0647\u064a \u0631\u062d\u0644\u0629\u064c \u062a\u0644\u062d\u0641\u062a\u0652 \u0628\u0623\u062d\u0632\u0627\u0646\u0650 \u0627\u0644\u063a\u0631\u0648\u0628 ..\r\n\u0648 \u062d\u0643\u0627\u064a\u0629\u064c \u0643\u064f\u062a\u0628\u062a\u0652 \u0639\u0644\u0649 \u062c\u062f\u0627\u0631\u0650 \u0627\u0644\u0642\u064e\u062f\u064e\u0631 .. \u0623\u0628\u0637\u0627\u0644\u0647\u0627 \u0623\u064a\u062a\u0627\u0645 .. \u0642\u062f \u063a\u0641\u0644 \u0639\u0646\u0647\u0645 \u0627\u0644\u0643\u062b\u064a\u064a\u064a\u064a\u0631 \u0645\u0646 \u0627\u0644\u0623\u0646\u0627\u0645 !\r\n\r\n\u0642\u062f \u0631\u0633\u0645\u0648\u0627 \u0623\u062d\u0644\u0627\u0645\u064e \u0627\u0644\u0643\u0628\u0627\u0631\u0650 \u0628\u0623\u064a\u062f\u064a\u0647\u0645 \u0627\u0644\u0645\u062a\u0639\u0628\u0629 .. \u0641\u0627\u0628\u062a\u0633\u0645\u0648\u0627 \u0628\u0634\u0641\u0627\u0647\u064d \u0634\u0627\u062d\u0628\u0629 ..\r\n\u062a\u063a\u0634\u0627\u0647\u0645 \u0645\u0633\u0627\u0621\u0627\u062a\u064c \u062a\u0628\u0643\u064a \u0627\u0646\u0643\u0633\u0627\u0631 \u0627\u0644\u062d\u064a\u0627\u0629 ..\r\n\u0648 \u064a\u0636\u0645\u0647\u0645 \u0628\u064a\u062a\u064c \u0634\u0647\u0650\u062f\u064e \u062a\u0641\u0627\u0635\u064a\u0644 \u0627\u0644\u0645\u0623\u0633\u0627\u0629 ..\r\n\u0648 \u062d\u0641\u0650\u0638\u064e \u0623\u0633\u0631\u0627\u0631 \u0627\u0644\u0643\u0631\u0627\u0645\u0629 !\r\n\r\n\r\n\u0641\u064a\u062f\u064a\u0648 \u0643\u0644\u064a\u0628 \u0633\u064a\u0646\u0645\u0627\u0626\u064a \u0645\u0646 \u0625\u0646\u062a\u0627\u062c \u0648\u0625\u0634\u0631\u0627\u0641 \u062c\u0645\u0639\u064a\u0629 \u062a\u0643\u0627\u0641\u0644 \u0627\u0644\u062e\u064a\u0631\u064a\u0629 \u0628\u0627\u0644\u0645\u062f\u064a\u0646\u0629 \u0627\u0644\u0645\u0646\u0648\u0631\u0629 takaful.sa","url":"http:\/\/www.youtube.com\/embed\/YyYmy79pKfs","views":"181736","likes":"1141"}]
答案 0 :(得分:0)
这是解析JSON数组的方法
JsonElement json = new JsonParser().parse(response);
JsonArray array = json.getAsJsonArray();
Iterator iterator = array.iterator();
List<CustomClass> obj_list = new ArrayList<CustomClass>();
while (iterator.hasNext()) {
JsonElement json2 = (JsonElement) iterator.next();
Gson gson = new Gson();
CustomClass obj = gson.fromJson(json2, CustomClass.class);
obj_list.add(obj);
}
此处,CustomClass
将是具有所需属性的类。这意味着您在Array中获得的属性,例如id,title,content,likes,views,url。
希望它有所帮助。
答案 1 :(得分:0)
JSON数据的最外层容器是一个数组而不是一个对象(它以方括号开头和结尾)。因此,您需要使用类JSONArray,然后遍历数组的所有元素:
String jsonString = ...
JSONArray arr = new JSONArray(jsonString);
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
String id = obj.getString("id");
String title = obj.getString("title");
...
}