2016-03-04 319 views
-2

我是json.My中的新问题,非常简单。我在我的json文件中有一些数组,也是一个字符串类型的数据。从Json获取字符串

现在我想要获取单个文本,名称:在我的java类中从json文件中反弹。 如何从json获取此字符串? 或其他任何方式来得到这个?

JSON文件

[{ 
     "title": "KalerKantha | OfftechIT", 
     "image": "", 
     "link": "http://www.kalerkantho.com/" 

    }, { 
     "title": "Prothom Alo | OfftechIT", 
     "image": "", 
     "link": "http://www.prothom-alo.com/" 
    }, 
    ... 
    { 
     "anounce": "I need this this text" 
    } 

] 

Java代码的

JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { 
    @Override 
    public void onResponse(JSONArray response) { 
     Log.d(TAG, response.toString()); 
     hidePDialog(); 

     // Parsing json 
     for (int i = 0; i < response.length(); i++) { 
      try { 

       JSONObject obj = response.getJSONObject(i); 

       Movie movie = new Movie(); 
       movie.setTitle(obj.getString("title")); 
       movie.setThumbnailUrl(obj.getString("image")); 
       movie.setLink(obj.getString("link").toString()); 
       // adding movie to movies array 
       movieList.add(movie); 


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

     } 

     // notifying list adapter about data changes 
     // so that it renders the Grid view with updated data 
     adapter.notifyDataSetChanged(); 
    } 
}, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
     if (error instanceof NoConnectionError){ 
      hidePDialog(); 
      Toast.makeText(getActivity(), " No Internet connection! \n Please check you connection", Toast.LENGTH_LONG).show(); 

     }}; 
}); 
+0

它总是会在数组的最后一个对象? – Rohit5k2

+0

不,这是一个单一的对象。 –

+0

检查obj.has(“anounce”),然后添加其他明智的忽略,你可以为所有对象做这个 –

回答

2

只需做检查宣布出现在JSON或不

 for (int i = 0; i < response.length(); i++) { 
     try { 

      JSONObject obj = response.getJSONObject(i); 

      if(obj.has("anounce")) 
      { 
      String anounce= obj.getString("anounce"); 

      } 
      else 
      { 
      Movie movie = new Movie(); 
      movie.setTitle(obj.getString("title")); 
      movie.setThumbnailUrl(obj.getString("image")); 
      movie.setLink(obj.getString("link").toString()); 
      // adding movie to movies array 
      movieList.add(movie); 

      } 

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

你的代码不工作:(... –

+0

@AovinMahmud ..你得到任何错误或异常,请让我知道 –

+0

没有文字:(滚动文字视图是黑色: –

1

使用optString方法。如果节点存在并且没有找到节点String.empty,它将返回值。

From Documentation

公共字符串optString(字符串名称)

返回通过名称映射的值,如果它存在,强迫它如果必要的话,或者如果没有这样的映射存在的空字符串。

做这个

try { 

    JSONObject obj = response.getJSONObject(i); 

    String announce = obj.optString("anounce"); 

    Movie movie = new Movie(); 
    movie.setTitle(obj.optString("title")); 
    movie.setThumbnailUrl(obj.optString("image")); 
    movie.setLink(obj.optString("link")); 
    // adding movie to movies array 
    movieList.add(movie); 

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

更新:使用optString是好多了,但如果你不想使用它,然后做这个

try { 

    JSONObject obj = response.getJSONObject(i); 

    if(i == response.length() -1) 
    { 
     String announce = obj.getString("anounce"); 
    } 
    else 
    { 
     Movie movie = new Movie(); 
     movie.setTitle(obj.getString("title")); 
     movie.setThumbnailUrl(obj.getString("image")); 
     movie.setLink(obj.getString("link").toString()); 
     // adding movie to movies array 
     movieList.add(movie); 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
+0

什么都没发生:( –

+0

在for循环后打印'announce'的值,它是否显示任何内容,为什么不使用我已经回答的第二个方法。 – Rohit5k2

+0

由Log.d它的工作 –

0

你的代码看起来象好,但您是否添加请求队列 像: -

AppController.getInstance().addToRequestQueue(movieReq); 

movieReq is JsonArrayRequest, 请检查。和

   String manounce=obj.getString("anounce"); 
1

你的JSON的结构很难。但是,如果你真的想要得到字符串。尝试这个。在for循环中添加这个。

String data = (obj.getString("announce") != null) ? obj.getString("announce") : ""; 
         // If you Movie has this annouce in your settler Getter if not try to add it. 
Movie.setAnnounce(data);