我有一个来自android Http的长字符串,如下所示:
{"movies":[
{"movieId":"fmen71229238","eTitle":"Mission: Impossible - Ghost Protocol","cTitle":"不可能的任務:鬼影行動","imageUrl":"http://test.mobibon.com.tw/MovieGoTest/Pics/pl_fmen7122923814_s.jpg","releaseDate":"2011/12/15","saleType":"0"},
{"movieId":"fstw79905171","eTitle":"Seediq Bale","cTitle":"賽德克.巴萊(上)太陽旗","imageUrl":"http://test.mobibon.com.tw/MovieGoTest/Pics/pl_fstw7990517114_s.jpg","releaseDate":"2011/9/9","saleType":"0"},
{"movieId":"fytw91390391","eTitle":"You Are the Apple of My Eye","cTitle":"那些年,我們一起追的女孩","imageUrl":"http://test.mobibon.com.tw/MovieGoTest/Pics/pl_fytw9139039102_s.jpg","releaseDate":"2011/8/19","saleType":"0"}
]}
字符串是JSON格式,我希望它在不同的数组中排序,并在Listview中显示,所以我使用了像这样的JSON paser
JSONArray result = new JSONArray(retSrc);
for(int i = 0;i < result.length(); i++)
{
JSONObject stock_data = result.getJSONObject(i);
Log.i("bird","eTitle:"+stock_data.getString("eTitle"));
}
} finally {
}
}
p.s retSrc是来自网站的长字符串
但是日志
Log.i("bird","eTitle:"+stock_data.getString("eTitle"));
什么都不记录。
我希望它像这样记录:
使命:不可能 - 幽灵协议Seediq Bale .....等
答案 0 :(得分:3)
这是解析它的代码
String jsonData = "{\"movies\":["
+ "{\"movieId\":\"fmen71229238\",\"eTitle\":\"Mission: Impossible - Ghost Protocol\",\"cTitle\":\"??????:????\",\"imageUrl\":\"http://test.mobibon.com.tw/MovieGoTest/Pics/pl_fmen7122923814_s.jpg\",\"releaseDate\":\"2011/12/15\",\"saleType\":\"0\"},"
+ "{\"movieId\":\"fstw79905171\",\"eTitle\":\"Seediq Bale\",\"cTitle\":\"???.??(?)???\",\"imageUrl\":\"http://test.mobibon.com.tw/MovieGoTest/Pics/pl_fstw7990517114_s.jpg\",\"releaseDate\":\"2011/9/9\",\"saleType\":\"0\"},"
+ "{\"movieId\":\"fytw91390391\",\"eTitle\":\"You Are the Apple of My Eye\",\"cTitle\":\"???,????????\",\"imageUrl\":\"http://test.mobibon.com.tw/MovieGoTest/Pics/pl_fytw9139039102_s.jpg\",\"releaseDate\":\"2011/8/19\",\"saleType\":\"0\"}"
+ "]}";
JSONObject jsonObj = new JSONObject(jsonData);
JSONArray movieArray = jsonObj.getJSONArray("movies");
JSONObject movieObj = null;
for (int i = 0; i < movieArray.length(); i++) {
movieObj = movieArray.optJSONObject(i);
if (null != movieObj) {
String mId = movieObj.optString("movieId");
String title = movieObj.optString("eTitle");
String cTitle = movieObj.optString("cTitle");
String imageUrl = movieObj.optString("imageUrl");
String releaseDate = movieObj.optString("releaseDate");
String saleType = movieObj.optString("saleType");
System.out.println("movieID [" + mId + "] eTitle [" + title
+ "] cTitle [" + cTitle + "] imgUrl [" + imageUrl
+ "] relDate [" + releaseDate + "] saleType ["
+ saleType + "]");
}
答案 1 :(得分:1)
你也可以使用Gson,它为你提供了Json解析的api。只需要创建所需的对象类型即可。
答案 2 :(得分:0)
显然,json文件的顶级不是JSONArray
而是JSONObject
。
使用以下内容:
JSONObject obj = new JSONOBject(retSrc);
JSONArray movieArray = obj.getJSONArray("movies");
//then process the movie as you did