Android:从服务器上的JSON文件中获取不带键的数组值

时间:2018-08-21 16:44:41

标签: android json android-volley

我想从此JSON文件链接JSON file link的数组内部的数组中获取值。我想从“元”和“视图”内部的“数据”数组中获取值。由于数据数组没有键,因此无法在应用程序中显示数据。

这是我的代码: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview"
        android:layout_width="347dp"
        android:layout_height="432dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="22dp"
        android:layout_marginTop="25dp"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="37dp"
        android:text="Button"
        tools:text="PowerBall result" />

</RelativeLayout>

MainActivity.java

package com.example.abina.getwebdata;

import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Website;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.textclassifier.TextClassification;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.ProtocolException;

import static android.provider.ContactsContract.CommonDataKinds.Website.URL;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private Button button;
    private RequestQueue requestQueue;

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

        textView = (TextView) findViewById(R.id.textview);
        button = (Button) findViewById(R.id.button);
        requestQueue = Volley.newRequestQueue(this);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resultParse();
            }
        });

    }

    public void  resultParse(){
        String url = "https://api.myjson.com/bins/y2oo8";

        final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("meta");

                            for (int i= 0;i<jsonArray.length();i++){
                                JSONObject dview = jsonArray.getJSONObject(i);
                                JSONArray jsonViewArray = dview.getJSONArray("view");

                                for (int l =0 ; l<jsonViewArray.length();l++){
                                    JSONObject columns = jsonViewArray.getJSONObject(l);
                                    JSONArray jsonColumnsArray = columns.getJSONArray("data");

                                    for (int k =0;k<jsonColumnsArray.length();k++){
                                        JSONObject data = jsonColumnsArray.getJSONObject(k);

                                        String name = data.toString();

                                        textView.append(name+"\n\n");
                                    }
                                }

                            }

                        } catch (JSONException e) {
                            Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_LONG).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_SHORT).show();
                error.printStackTrace();
            }
        });

        requestQueue.add(request);
    }
}

2 个答案:

答案 0 :(得分:1)

仅供参考,"data"数组不在"meta" and "view"内部。它位于"meta"所在级别的顶部。

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

第二个问题的答案::如果要显示data json数组的第一个元素,请执行以下操作

JSONArray jsonArray = response.getJSONArray("data");
if (jsonArray!=null && jsonArray.length() > 0) {
    String data = jsonArray.optString(0, null);
    textView.setText(data);
}

结果是

enter image description here

答案 1 :(得分:0)

  try {
            // Parse the data into jsonobject to get original data in form of json.
            JSONObject jsonResponse = new JSONObject(result);
            JSONArray jsonArrayData = jsonResponse.getJSONArray("data");
            for(int i=0;i<jsonArrayData.length();i++){
                JSONArray jsonItem = jsonArrayData.getJSONArray(i);
                //take each one of them from the jsonarray and use it
                String zero = jsonItem.get(0).toString();
                String one = jsonItem.get(1).toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }