【问题标题】:Getting string from Json从 Json 获取字符串
【发布时间】:2016-03-04 09:05:53
【问题描述】:

我是 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();

        }};
});

【问题讨论】:

  • 它是否总是在数组的最后一个对象中?
  • 不,这是一个单一的对象。
  • 检查 obj.has("anounce") 然后添加其他明智的忽略您可以对所有对象执行此操作
  • 然后发布完整的 json。从您的 json 来看,它看起来像是数组的最后一个元素。
  • 你可能对Gson感兴趣,太棒了。

标签: java android json string


【解决方案1】:

您只需检查 announce 是否存在于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();
        }

【讨论】:

  • @AovinMahmud ..您是否遇到任何错误或异常,请告诉我
  • 没有文本:(滚动文本视图是黑色的:(
  • scrollingtext = (TextView) view.findViewById(R.id.scroll_annouc); scrollingtext.setText(anounce); scrollingtext.setSelected(true);
【解决方案2】:

使用optString 方法。如果节点存在并且String.empty is node is not found,它将返回该值。

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

【讨论】:

  • 在for循环后打印announce的值。有没有显示什么。为什么不使用我回答的第二种方法。
  • 如何在我的滚动文本中设置它。下面的代码不起作用。scrollingtext = (TextView) view.findViewById(R.id.scroll_annouc); scrollingtext.setText(anounce); scrollingtext.setSelected(true);
  • 在类级别定义 String announce 并在 try 块中使用此变量。您尝试使用哪种方法设置值?
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

    AppController.getInstance().addToRequestQueue(movieReq);
    

    movieReq 是 JsonArrayRequest, 请检查一下。和

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

    【讨论】:

      猜你喜欢
      • 2016-03-19
      • 2022-09-24
      • 2016-03-12
      • 2020-11-10
      • 2016-09-30
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多