【问题标题】:How to parse a JSON file with volley如何使用 volley 解析 JSON 文件
【发布时间】:2018-01-14 22:55:51
【问题描述】:

我正在开发一个在线广播应用演示。我需要从互联网上获取一些数据,例如电台名称和口号,我看了一些关于如何使用 volley 解析 JSON 文件并从互联网上获取数据的教程,但我尝试了不同的方法,但似乎没有任何效果,这是一个简化的代码

代码

public class MainActivity extends AppCompatActivity {
    TextView textView;
    RequestQueue requestQueue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestQueue = Volley.newRequestQueue(getApplicationContext());

        textView =(TextView) findViewById(R.id.textView);



        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("https://api.myjson.com/bins/cd6dh", null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("1");
                            for (int i = 0; i <= jsonArray.length(); i++) {
                                JSONObject station = jsonArray.getJSONObject(i);
                                String stationname = station.getString("motto");
                                textView.append(stationname);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("volley", "error");

                    }
                });

            requestQueue.add(jsonObjectRequest);

}

JSON 网址 https://api.myjson.com/bins/cd6dh

当我运行这段代码时,我的文本仍然保持不变

【问题讨论】:

  • i &lt;= jsonArray.length() 在索引数组时会抛出越界异常。也许这就是它不起作用的原因?
  • 你也不应该使用数字作为对象键...直接使用数组的索引

标签: android json android-volley android-json


【解决方案1】:

您的数据不是JSONObject,而是JSONArray,因此您将使用来自VolleyJSONArrayRequest,而不是像您正在使用的JSONObjectRequest。您可能会在那里遇到一个异常。在您获得JSONArray 后,此线路将为您提供第一站的

JSONObject station = jsonArray.getJSONObject(0).getJSONArray("1").getJSONObject(0);

station.getString("motto"); 

会给你车站的座右铭。

这是因为你的数据结构非常复杂。我建议让它更好更容易导航


这就是你的请求代码 sn-p 应该是这样的

  JsonArrayRequest jsonArrayRequest = new JsonArrayRequest("https://api.myjson.com/bins/cd6dh",
      new Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
          try {
            JSONObject station1 = response.getJSONObject(0).getJSONArray("1").getJSONObject(0);
            String stationName = station1.getString("motto");
          } catch (JSONException e) {
            e.printStackTrace();
          }
        }
      },
      new ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
          Log.e("volley", "error");
        }
      });
      requestQueue.add(jsonArrayRequest);

事实证明,除了错误的请求之外,您也从未分配过侦听器。它作为您请求的第二个参数为 null。

【讨论】:

  • 我刚刚尝试了代码,结果很完美,我真的很感激。谢谢
【解决方案2】:

你可以试试这个:-

JSONArray jsonArray = new JSONArray(response+""); // string is the most general datatype
Log.d("MyApp",jsonArray+"");

在这个地方

JSONArray jsonArray = response.getJSONArray("1");

这里 - String stationname = station.getString("motto");

可以

int stringIndex              = (i+1); // your jsonarray element are one more than the index
String key                    = String.valueOf(stringIndex); // convert the incremented index to string
JSONArray index        = new JSONArray(station.getString(key));// get string from the jsonobject and convert it to jsonarray
JSONObject indexed  = index.getJSONObject(0); // get the first object from the json array
String stationname      = indexed.getString("motto"); // finally get the motto value  

注意:- 您的 json 数组不好,因为您根据数字进行索引,它可能是数组中的直接 json 对象...

希望对你有帮助:)

【讨论】:

  • 嗨,我复制并粘贴了您的代码,但出现错误,您可以在 link 看到它
  • @OpeyemiSanusi 检查我的答案。该代码应该适用于它
  • 使用一次...new JSONArray(station.getString(key));我已经编辑了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多