java - 足球数据 API JSON 解析错误

标签 java android json api parsing

我在从 ( http://www.football-data.org ) 下载足球数据时遇到困难。我正在尝试通过为每个球队创建一个新的 Standing 对象并将它们添加到 ArrayList 来下载英超联赛表。

我已经设置了我的解析器类、适配器类和我的 View 。

我收到的错误是:

org.json.JSONException: org.json.JSONException: Value {"_links":{"self":{"href":"http:\/\/api.football-data.org\/v1\/soccerseasons\/426\/leagueTable\/?matchday=37"},"soccerseason":{"href":"http:\/\/api.football-data.org\/v1\/soccerseasons\/426"}},"leagueCaption":"Premier League 2016\/17","matchday":37,"standing":[{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/61"}},"position":1,"teamName":"Chelsea FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/de\/5\/5c\/Chelsea_crest.svg","playedGames":36,"points":87,"goals":76,"goalsAgainst":29,"goalDifference":47,"wins":28,"draws":3,"losses":5,"home":{"goals":46,"goalsAgainst":13,"wins":15,"draws":0,"losses":2},"away":{"goals":30,"goalsAgainst":16,"wins":13,"draws":3,"losses":3}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/73"}},"position":2,"teamName":"Tottenham Hotspur FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/de\/b\/b4\/Tottenham_Hotspur.svg","playedGames":35,"points":77,"goals":71,"goalsAgainst":23,"goalDifference":48,"wins":23,"draws":8,"losses":4,"home":{"goals":45,"goalsAgainst":8,"wins":16,"draws":2,"losses":0},"away":{"goals":26,"goalsAgainst":15,"wins":7,"draws":6,"losses":4}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/65"}},"position":3,"teamName":"Manchester City FC","crestURI":"https:\/\/upload.wikimedia.org\/wikipedia\/en\/e\/eb\/Manchester_City_FC_badge.svg","playedGames":36,"points":72,"goals":72,"goalsAgainst":38,"goalDifference":34,"wins":21,"draws":9,"losses":6,"home":{"goals":34,"goalsAgainst":16,"wins":10,"draws":7,"losses":1},"away":{"goals":38,"goalsAgainst":22,"wins":11,"draws":2,"losses":5}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/64"}},"position":4,"teamName":"Liverpool FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/de\/0\/0a\/FC_Liverpool.svg","playedGames":36,"points":70,"goals":71,"goalsAgainst":42,"goalDifference":29,"wins":20,"draws":10,"losses":6,"home":{"goals":42,"goalsAgainst":18,"wins":11,"draws":5,"losses":2},"away":{"goals":29,"goalsAgainst":24,"wins":9,"draws":5,"losses":4}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/57"}},"position":5,"teamName":"Arsenal FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/en\/5\/53\/Arsenal_FC.svg","playedGames":35,"points":66,"goals":68,"goalsAgainst":42,"goalDifference":26,"wins":20,"draws":6,"losses":9,"home":{"goals":34,"goalsAgainst":15,"wins":12,"draws":3,"losses":2},"away":{"goals":34,"goalsAgainst":27,"wins":8,"draws":3,"losses":7}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/66"}},"position":6,"teamName":"Manchester United FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/de\/d\/da\/Manchester_United_FC.svg","playedGames":35,"points":65,"goals":51,"goalsAgainst":27,"goalDifference":24,"wins":17,"draws":14,"losses":4,"home":{"goals":24,"goalsAgainst":12,"wins":7,"draws":10,"losses":1},"away":{"goals":27,"goalsAgainst":15,"wins":10,"draws":4,"losses":3}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/62"}},"position":7,"teamName":"Everton FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/de\/f\/f9\/Everton_FC.svg","playedGames":37,"points":61,"goals":61,"goalsAgainst":41,"goalDifference":20,"wins":17,"draws":10,"losses":10,"home":{"goals":42,"goalsAgainst":16,"wins":13,"draws":4,"losses":2},"away":{"goals":19,"goalsAgainst":25,"wins":4,"draws":6,"losses":8}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/74"}},"position":8,"teamName":"West Bromwich Albion FC","crestURI":"http:\/\/upload.wikimedia.org\/wikipedia\/de\/8\/8b\/West_Bromwich_Albion.svg","playedGames":36,"points":45,"goals":41,"goalsAgainst":46,"goalDifference":-5,"wins":12,"draws":9,"losses":15,"home":{"goals":27,"goalsAgainst":22,"wins":9,"draws":2,"losses":8},"away":{"goals":14,"goalsAgainst":24,"wins":3,"draws":7,"losses":7}},{"_links":{"team":{"href":"http:\/\/api.football-data.org\/v1\/teams\/1044"}},"position":9,"teamName":"AFC Bournemouth","crestURI":"https:\/
05-13 15:39:24.798 6021-6540/com.example.oisin.premierleaguesocial W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
05-13 15:39:24.798 6021-6540/com.example.oisin.premierleaguesocial W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:96)
05-13 15:39:24.798 6021-6540/com.example.oisin.premierleaguesocial W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108)
05-13 15:39:24.798 6021-6540/com.example.oisin.premierleaguesocial W/System.err:     at com.example.oisin.premierleaguesocial.Utilities.JSONParser.parse(JSONParser.java:75)
05-13 15:39:24.799 6021-6540/com.example.oisin.premierleaguesocial W/System.err:     at com.example.oisin.premierleaguesocial.Utilities.JSONParser.doInBackground(JSONParser.java:56)
05-13 15:39:24.799 6021-6540/com.example.oisin.premierleaguesocial W/System.err:     at com.example.oisin.premierleaguesocial.Utilities.JSONParser.doInBackground(JSONParser.java:27)

我已经尝试解决该问题几个小时了,但无法解决。我认为我已经正确设置了数据类型。为了简单起见,我决定首先尝试获取整数数据类型。

任何帮助将不胜感激。谢谢。

public class JSONParser extends AsyncTask<Void, Void, Boolean> {


    private Context c;
    private String jsonData;
    private RecyclerView rv;

    private ProgressDialog pd;
    private ArrayList<Standing> mLeagueTable = new ArrayList<>();

    public JSONParser(Context c, String jsonData, RecyclerView rv) {
        this.c = c;
        this.jsonData = jsonData;
        this.rv = rv;
    }

    @Override
    protected void onPreExecute () {
        super.onPreExecute();

        pd = new ProgressDialog(c);
        pd.setTitle("Parse");
        pd.setMessage("Parsing..Please Wait");
        pd.show();
    }

    @Override
    protected Boolean doInBackground (Void...params){

        return parse();
    }

    @Override
    protected void onPostExecute (Boolean isParsed){
        super.onPostExecute(isParsed);

        pd.dismiss();   //Dismiss progress dialog.
        if (isParsed) {
            //BIND
            rv.setAdapter(new TableAdapter(c, mLeagueTable));   //Pass in instance of adapter.

        } else {
            Toast.makeText(c, "Unable to Parse check your Log Output", Toast.LENGTH_SHORT).show();
        }
    }

    private Boolean parse() {
        try {
            //JSONArray ja = new JSONArray(jsonData);
            JSONArray ja = new JSONArray("standing");
            JSONObject jo;  //declare json OBJECT

            mLeagueTable.clear();  //Clears the ArrayList.
            Standing table;      //Declare a table

            for (int i = 0; i < ja.length(); i++)  //iterating in JSONArray
            {
                jo = ja.getJSONObject(i);


                int position = ja.getInt(Integer.parseInt("position"));
                int points = ja.getInt(Integer.parseInt("points"));
                int playedGames = ja.getInt(Integer.parseInt("playedGames"));
                int goals = ja.getInt(Integer.parseInt("goals"));
                int goalsAgainst = ja.getInt(Integer.parseInt("goalsAgainst"));
                int goalDifference = ja.getInt(Integer.parseInt("goalDifference"));



                table = new Standing();      //Create a new "User" object.

                table.setPosition(position);
                table.setPoints(points);
                table.setPlayedGames(playedGames);
                table.setGoals(goals);
                table.setGoalsAgainst(goalsAgainst);
                table.setGoalDifference(goalDifference);


                mLeagueTable.add(table);    //Add new Standing object to ArrayList mLeagueTable.
            }
            return true;

        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        }
    }
}







public class JSONDownloader extends AsyncTask<Void, Void, String> {

    Context c;
    String jsonURL;
    RecyclerView rv;


    ProgressDialog pd;


    public JSONDownloader(Context c, String jsonURL, RecyclerView rv) {
        this.c = c;
        this.jsonURL = jsonURL;
        this.rv = rv;
    }




    @Override
    protected void onPreExecute() { //CALLED just before data is downloaded.
        super.onPreExecute();

        pd = new ProgressDialog(c);
        pd.setTitle("Download JSON");
        pd.setMessage("Downloading.... Please Wait!");
        pd.show();
    }

    @Override
    protected String doInBackground(Void... voids) {
        return download();
    }

    @Override
    protected void onPostExecute(String jsonData) {
        super.onPostExecute(jsonData);

        pd.dismiss();
        if (jsonData.startsWith("Error"))
        {
            String error = jsonData;
            Toast.makeText(c, error, Toast.LENGTH_SHORT).show();
        } else {

            //PARSER
            new JSONParser(c, jsonData, rv).execute();

        }
    }

    private String download() {
        Object connection = Connector.connect(jsonURL);
        if (connection.toString().startsWith("Error")) {
            return connection.toString();
        }

        try {
            HttpURLConnection con = (HttpURLConnection) connection; //Cast connection to HTTPConnection
            if (con.getResponseCode() == con.HTTP_OK) {
                //GET INPUT FROM STREAM
                InputStream is = new BufferedInputStream(con.getInputStream());
                BufferedReader br = new BufferedReader(new InputStreamReader(is));

                String line;
                StringBuffer jsonData = new StringBuffer();

                // READ
                while ((line = br.readLine()) != null) {
                    jsonData.append(line + "\n");
                }

                //CLOSE RESOURCES
                br.close();
                is.close();

                //RETURN JSON
                return jsonData.toString();
            } else {
                return "Error " + con.getResponseMessage();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "Error " + e.getMessage();
        }
    }
}


 public class MainActivity extends AppCompatActivity {

        String jsonURL = "http://api.football-data.org/v1/soccerseasons/426/leagueTable";

        public static final String TAG = "MainActivity";
        //FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

        private RecyclerView rv;
        /*private ArrayList<Standing> mLeagueTable = new ArrayList<>(); //Initalise m
        private TableAdapter mTableAdapter = new TableAdapter(mLeagueTable, this); */

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_table);
            //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            //setSupportActionBar(toolbar);

            rv = (RecyclerView) findViewById(R.id.rv);
            if (rv != null) {
                rv.setLayoutManager(new LinearLayoutManager(this));
            }
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);   //Initialze FAB
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new JSONDownloader(MainActivity.this, jsonURL, rv).execute();
                }
            });
        }
    }



public class TableAdapter extends RecyclerView.Adapter<MyViewHolder>{

    private ArrayList<Standing> mLeagueTable;
    private Context c;


    public TableAdapter(Context c, ArrayList<Standing> mLeagueTable) {
        this.c = c;
        this.mLeagueTable = mLeagueTable;

    } //Constructor



    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.tableitem, parent, false);    //Inlfate the League Table model view.
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {   //BINDS THE data to the appropriote View


            Standing  s = mLeagueTable.get(position);

            final int teamPosition = s.getPosition();
            final String teamName = s.getTeamName();
            final int playedGames = s.getPlayedGames();
            final int goals = s.getGoals();
            final int goalsAgainst = s.getGoalsAgainst();
            final int goalDifference = s.getGoalDifference();
            final int points = s.getPoints();




            //holder.teamNameTxt.setText(teamName);
            holder.teamPositionTxt.setText(teamPosition);
            holder.playedGamesTxt.setText(playedGames);
            //holder.teamImage.setImageResource(s.getTeamImage());
            holder.goalsTxt.setText(goals);
            holder.goalsAgainstTxt.setText(goalsAgainst);
            holder.goalDifferenceTxt.setText(goalDifference);
            holder.pointsTxt.setText(points);
           // holder.wins.setText(s.getWins());
            //holder.draws.setText(s.getDraws());
            //holder.losses.setText(s.getLosses());


        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int pos) {
                openActivity(teamPosition, playedGames, goals, goalsAgainst, goalDifference, points);
            }
        });



    }



    @Override
    public int getItemCount() {return mLeagueTable.size();}




    ////open Activity
    private void openActivity(int...details)
    {
        Intent i = new Intent(c, TableActivity.class);
        i.putExtra("POSITION_KEY", details[0]);
        //i.putExtra("TEAMNAME_KEY", details[1]);
        i.putExtra("POINTS_KEY", details[2]);
        i.putExtra("PLAYEDGAMES_KEY", details[3]);
        i.putExtra("GOALS_KEY", details[4]);
        i.putExtra("GOALSAGAINST_KEY", details[5]);
        i.putExtra("GOALDIFFERENCE_KEY", details[6]);

        c.startActivity(i);
    }
}

表项.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/teamPositionTxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/primary_text" />


    <TextView
        android:id="@+id/teamNameTxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:textColor="@color/primary_text" />

    <TextView
        android:id="@+id/pointsTxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/primary_text"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/playedGamesTxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/primary_text" />

    <TextView
        android:id="@+id/goalsTxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/primary_text" />

    <TextView
        android:id="@+id/goalsAgainstTxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/primary_text" />

    <TextView
        android:id="@+id/goalDifferenceTxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textColor="@color/primary_text" />

</LinearLayout>

最佳答案

1. 从字符串 jsonData 创建一个 JSONObject

2. 使用 jsonObj.getJSONArray("从 JSONObject 获取 JSONArray (standing)站立”)

3.for 循环中,使用 jo.getInt( 从 JsonObject 获取 int 值) 而不是 ja.getInt()

这是工作代码:

private Boolean parse() {

    try {
        JSONObject jsonObj = new JSONObject(jsonData);

        JSONArray ja = jsonObj.getJSONArray("standing");

        mLeagueTable.clear();  //Clears the ArrayList.

        for (int i = 0; i < ja.length(); i++)  //iterating in JSONArray
        {
            JSONObject jo = ja.getJSONObject(i);

            int position = jo.getInt(Integer.parseInt("position"));
            int points = jo.getInt(Integer.parseInt("points"));
            int playedGames = jo.getInt(Integer.parseInt("playedGames"));
            int goals = jo.getInt(Integer.parseInt("goals"));
            int goalsAgainst = jo.getInt(Integer.parseInt("goalsAgainst"));
            int goalDifference = jo.getInt(Integer.parseInt("goalDifference"));

            Standing table = new Standing();      //Create a new "User" object.

            table.setPosition(position);
            table.setPoints(points);
            table.setPlayedGames(playedGames);
            table.setGoals(goals);
            table.setGoalsAgainst(goalsAgainst);
            table.setGoalDifference(goalDifference);


            mLeagueTable.add(table);    //Add new Standing object to ArrayList mLeagueTable.
        }
        return true;

    } catch (JSONException e) {
        e.printStackTrace();
        return false;
    }
}

希望这会起作用~

关于java - 足球数据 API JSON 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43954434/

相关文章:

Java:不区分大小写的枚举 Jersey 查询参数绑定(bind)

java - 如何在Android上加载XML进行自定义解析?

javascript - 合并两个 json 数组

javascript - Angularjs + 停止将数据更新到变量中

java - hasnext() 如何在 Java 中的集合中工作

java - 将数字拆分为多个数字

java - 在android中收到来自firebase的消息时如何创建事件监听器?

android - Cordova Facebook 插件 - 图形 API 版本?

java - OnclickListener 无法识别上下文方法

php - 如何在 JSON 中编码 MYSQL 响应并在 iOS 应用程序中解析