如何转换为json格式并从json数组中获取jsonobject

时间:2016-10-08 07:58:02

标签: android json

[{"id":0,"date":"4 - 0 - 1900","subject":"dfhjkjdfhgd","desc":"dfgbjdfiobhfh"},{"id":1,"date":"12
- 0 - 1900","subject":"4564546","desc":"fdghsdfhbrtfh"},{"id":2,"date":"6 - 0 - 1900","subject":"gfgjfcghnf","desc":"fgbnfgh"},{"id":3,"date":"11
- 0 - 1900","subject":"fgjhnfgjhnfg","desc":"fghfhn"}]

这是我必须在json数组中解析json对象的字符串。请帮我找到解决方案。

2 个答案:

答案 0 :(得分:0)

您可以使用GSON或Jackson来解析JSON数组

示例:

Gson gson = new Gson(); String jsonOutput = response; Type listType = new TypeToken<List<your_POJO_class>>(){}.getType(); List<your_POJO_class> mylist = (List<your_POJO_class>)gson.fromJson(jsonOutput, listType);

答案 1 :(得分:0)

在doInBackground中

JSONArray response;    
response = new JSONArray(responseString); // responseString is your Value

String ID[];
String Date[];
String Subject[];
String Desc[];

在OnPostExecute中

ID= new String[response.length()];
Date = new String[response.length()];
Subject = new String[response.length()];
Desc = new String[response.length()];
for(int i = 0 , count = response.length(); i < count ; i++ )
{
  try
  {
   JSONObject jObj = response.getJSONObject(i);
   ID[i] = jObj.getString("id");
   Date[i] = jObj.getString("date");
   Subject[i] = jObj.getString("subject");
   Desc[i] = jObj.getString("desc");
  }
  catch(JSONException e)
  {}
}