gson 如何在android中从json对象url获取值

b4wnujal  于 2023-03-02  发布在  Android
关注(0)|答案(1)|浏览(201)

我正在尝试从一个JSON获取URL

{
   "file_length":300,
   "versions":[
      {
         "size":38267700,
         "url":"https:\/\/sto006.sh-content.com\/v\/01\/00158\/v4h6zad7kdr1_n\/v4h6zad7kdr1_n.mp4?t=x7xUQpMLNKmr2glDAIN6YetW_hucsU9gzLOq6ifOuWw&s=1677397458&e=129600&f=790153&sp=300&i=0.0",
         "name":"n"
      }
   ],
   "player_img":"https:\/\/sh-content.xyz\/v4h6zad7kdr1_xt.jpg"
}

我只想从这个json文件url中获取那个url:https://api.streamhide.com/api/file/direct_link?key=1208rmfu7uutkadgs5rx&file_code=v4h6zad7kdr1
我尝试了什么:

RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Url, null, response -> {
        try {
            String url = response.getString("result");
         Log.d("name",url);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }, error -> {

        Toast.makeText(MainActivity.this, "Fail to get data..", Toast.LENGTH_SHORT).show();
    });

    queue.add(jsonObjectRequest);

请帮助我执行以下操作“从json文件获取url对象““url”:“https://sto006.sh-content.com“

snz8szmq

snz8szmq1#

解决方案包括解析一位结果变量,如下所示:

RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
        String Url = "https://api.streamhide.com/api/file/direct_link?key=1208rmfu7uutkadgs5rx&file_code=v4h6zad7kdr1";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Url, null, response -> {
            try {

                JSONObject responseJSONObject = response.getJSONObject("result");
                JSONArray array = responseJSONObject.getJSONArray("versions");
                JSONObject obj = (JSONObject) array.get(0);
                String url = obj.getString("url");
                Log.d("name",url);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }, error -> {

            Toast.makeText(MainActivity.this, "Fail to get data..", Toast.LENGTH_SHORT).show();
        });

        queue.add(jsonObjectRequest);

相关问题