如何使用 volley 解析 JSON 文件

How to parse a JSON file with volley

我正在制作在线广播应用程序演示。我需要从互联网上获取一些数据,比如电台的名称和口号,我看了一些关于如何使用 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 URL https://api.myjson.com/bins/cd6dh

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

您的数据不是 JSONObject,而是 JSONArray,因此您可以使用 Volley 中的 JSONArrayRequest 而不是像现在这样的 JSONObjectRequest使用。您可能在那里遇到了异常。在你得到 JSONArray 之后,这条线会给你你的 station 第一站

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

station.getString("motto"); 

会给你站的座右铭。

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


这就是您的请求代码段的样子

  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。

你能试试这个吗:-

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 个对象...

希望对你有所帮助:)