JsonArray和JsonData实现

时间:2017-09-05 19:17:13

标签: android json

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mProgressBar.setVisibility(View.INVISIBLE);

        String soccerURL = "http://api.football-data.org/v1/competitions/444/leagueTable";

        if (isNetworkAvailable()) {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(soccerURL)
                    .build();

            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {

                }

                @Override
                public void onResponse(Response response) throws IOException {
                    try {
                        String jsonData = response.body().string();
                        Log.v(TAG, jsonData );
                        if (response.isSuccessful()) {
                            mTableSoccer = getCurrentDetails(jsonData);
                        } else {
                            alertUserAboutError();
                        }
                    }
                    catch (IOException e) {
                        Log.e(TAG, "Exception caught: ", e);
                    }
                    catch (JSONException e){
                        Log.e(TAG, "Exception caught: ", e);
                    }

                }
            });

        }
        else {
            Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show();
        }

        Log.d(TAG, "Main UI code is running!");

    }

    private TableSoccer getCurrentDetails(String jsonData) throws JSONException {

            JSONObject table = new JSONObject(jsonData);
            String leagueCaption = table.getString("leagueCaption");
            Log.i(TAG, "From Json: " + leagueCaption);

            JSONObject standing = table.getJSONObject("standing");

            TableSoccer tableSoccer = new TableSoccer();
            tableSoccer.setmPosition(standing.getDouble("position"));
            tableSoccer.setmPoints(standing.getDouble("points"));
            tableSoccer.setmWins(standing.getDouble("wins"));
            tableSoccer.setmLosses(standing.getDouble("losses"));


            return tableSoccer;

    }

    private boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        boolean isAvailable =  false;
        if (networkInfo !=null && networkInfo.isConnected()){
            isAvailable = true;
        }
        return isAvailable;
    }

    private void alertUserAboutError() {
        AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.show(getFragmentManager(), "error_dialog");
    }
}

如何从此Json Array链接获取数据 http://api.football-data.org/v1/competitions/444/leagueTable, ,我尝试代码来显示位置,积分,目标,胜利,平局,损失,球队名称,目标对抗。我被困在JsonObject和JsonData中的public void onResponse。我也有桌上足球课,并在那里设置好一切。感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

首先,你应该看看at this它会让你了解JSON格式以及如何使用它。

阅读完后,您会了解到standing元素是JsonArray而不是JsonObject。因此,您应首先获取数组并开始在其中提取对象并根据要求使用它。下面的代码可能会帮助您搞清楚。

    JSONObject table = new JSONObject(jsonData);
    String leagueCaption = table.getString("leagueCaption");
    Log.i(TAG, "From Json: " + leagueCaption);
    JSONArray standingArray = table.getJsonArray("standing");

    //this will be used to hold all the extracted objects inside standingArray.
    ArrayList<TableSoccer> tableSoccerElements = new ArrayList<>();

    for(int i=0; i< standingArray.length(); i++){

        JSONObject standingObject = standingArray.getJSONObject(i);

        TableSoccer tableSoccer = new TableSoccer();
        tableSoccer.setmPosition(standingObject.getDouble("position"));
        tableSoccer.setmPoints(standingObject.getDouble("points"));
        tableSoccer.setmWins(standingObject.getDouble("wins"));
        tableSoccer.setmLosses(standingObject.getDouble("losses"));

        tableSoccerElements.add(tableSoccer);

    }

    //here your tableSoccerElements array will be filled with the respective response number
    //elements which is ready to use.