从Json获取字符串

时间:2016-03-04 09:05:53

标签: java android json string

我是json的新手。我的问题非常简单。我的json文件中有一些数组,也是字符串类型的数据。

现在我想从json文件中获取我的java类中的单个文本,名称: anounce 。 我如何从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();

        }};
});

4 个答案:

答案 0 :(得分:3)

您只需检查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();
        }

答案 1 :(得分:1)

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

From Documentation

  

public String optString(String name)

     

返回按名称映射的值(如果存在),必要时强制转换它,如果不存在这样的映射,则返回空字符串。

这样做

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();
}

答案 2 :(得分: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);

答案 3 :(得分:0)

您的代码看起来不错但是您是否将请求添加到队列中 如: -

AppController.getInstance().addToRequestQueue(movieReq);

movieReq是JsonArrayRequest, 请检查一下。和

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