我在Android中获得Jason回复的经验不足。我的问题是如何解析来自此API {J}的JSON。基本上是为了介绍,我需要参加活动。然后我想创建一个匹配列表并从中创建https://api.the-odds-api.com/v2/odds/?sport=UPCOMING®ion=uk&apiKey=e3b70c1881a5d9eec34e4cb256844874
。
这是我与API沟通的课程
RecyclerView
不知何故,我参加了这些活动,但不知道如何继续这项活动。我需要一些事件,因为我想创建Match.class(String homeTeam,String awayTeam)的任意对象,并将其传递给循环视图。我在这个JsonTask上这样做,我认为这不是最好的方法。private class JsonTask extends AsyncTask<String, String, String> implements OnVisibleCallback{
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line+"\n");
Log.d("Response: ", "> " + line); //here u ll get whole response...... :-)
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
Log.d("json",response);
try {
JSONObject jObject = new JSONObject(response);
String data = jObject.getString("data");
JSONObject objData = new JSONObject(data);
String events = objData.getString("events");
JSONObject eventObj = new JSONObject(events);
int numberOfEvents = eventObj.length();
List<Match> matchList= Arrays.asList(new Match("Juventus","Fiorentina"));
counter = 1;
Log.d("Counter_Ticket_Fragment", String.valueOf(counter));
mAdapter = new MatchAdapter(matchList,counter);
changeVisible();
mAdapter.setCallback(this);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void changeVisible() {
if(mAdapter.getItemCount()>0){
noMatch.setVisibility(View.GONE);
}
else{
noMatch.setVisibility(View.VISIBLE);
}
}
}
在JsonTask
OnCreateView
中被调用。
这是Match.class
Fragment
这是MatchAdapter
public class Match {
private String team_home;
private String team_away;
public Match(String team_home, String team_away){
this.team_home = team_home;
this.team_away = team_away;
}
public String getHomeTeam() {
return team_home;
}
public String getAwayTeam() {
return team_away;
}
}
答案 0 :(得分:0)
我建议您更改后端json数据,因为数据结构不合理。
创建一个事件数组,其中数组的每个元素都是一个对象,然后使用下面的代码解析数据
JSONObject jObject = new JSONObject(response);
String data = jObject.getString("data");
JSONObject objData = new JSONObject(data);
String events = objData.getString("events");
JSONArray jsonEvent =
objData.getJsonArray(events);
for(int i=0; i<jsonEvent.length; I++){
JsonObject jsonObj = jsonEvent.getJsonObject(i);
}
答案 1 :(得分:0)
JSONObject jObject = new JSONObject(response);
String data = jObject.getString("data");
JSONObject objData = new JSONObject(data);
String events = objData.getString("events");
JSONObject eventObj = new JSONObject(events);
int numberOfEvents = eventObj.length();
for(int i = 0; i<numberOfEvents; i++){
event = eventObj.names().getString(i);
String matchOne = eventObj.getString(event);
JSONObject singleEventObj = new JSONObject(matchOne);
JSONArray participants = singleEventObj.getJSONArray("participants");
}
我确实迭代了事件名称(键),现在一切都按照要求进行了