【发布时间】:2018-07-31 19:47:57
【问题描述】:
我要解析的 JSON。 我要解析的两个 JSON 数据。并提供了我的 mainActivty 代码,请看一下。 我已经访问了 JSON 数组并对其进行循环获取第一个元素的对象并尝试从第一个元素中获取详细信息,不幸的是我无法获取它们。我试图记录从 postExecute 返回的 arrayListed。它仍然显示 size =0..
[
{
"score": 24.910917,
"show": {
"id": 13,
"url": "http://www.tvmaze.com/shows/13/the-flash",
"name": "The Flash",
"type": "Scripted",
"language": "English",
"genres": [
"Drama",
"Action",
"Science-Fiction"
],
"status": "Running",
"runtime": 60,
"premiered": "2014-10-07",
"officialSite": "http://www.cwtv.com/shows/the-flash/",
"schedule": {
"time": "20:00",
"days": [
"Tuesday"
]
},
"rating": {
"average": 8.1
},
"weight": 99,
"network": {
"id": 5,
"name": "The CW",
"country": {
"name": "United States",
"code": "US",
"timezone": "America/New_York"
}
},
"webChannel": null,
"externals": {
"tvrage": 36939,
"thetvdb": 279121,
"imdb": "tt3107288"
},
"image": {
"medium": "http://static.tvmaze.com/uploads/images/medium_portrait/129/323466.jpg",
"original": "http://static.tvmaze.com/uploads/images/original_untouched/129/323466.jpg"
},
"summary": "<p>After a particle accelerator causes a freak storm, CSI Investigator Barry Allen is struck by lightning and falls into a coma. Months later he awakens with the power of super speed, granting him the ability to move through Central City like an unseen guardian angel. Though initially excited by his newfound powers, Barry is shocked to discover he is not the only \"meta-human\" who was created in the wake of the accelerator explosion -- and not everyone is using their new powers for good. Barry partners with S.T.A.R. Labs and dedicates his life to protect the innocent. For now, only a few close friends and associates know that Barry is literally the fastest man alive, but it won't be long before the world learns what Barry Allen has become...The Flash!</p>",
"updated": 1532242720,
"_links": {
"self": {
"href": "http://api.tvmaze.com/shows/13"
},
"previousepisode": {
"href": "http://api.tvmaze.com/episodes/1426878"
},
"nextepisode": {
"href": "http://api.tvmaze.com/episodes/1473791"
}
}
}
},
{
"score": 22.392826,
"show": {
"id": 528,
"url": "http://www.tvmaze.com/shows/528/the-flash",
"name": "The Flash",
"type": "Scripted",
"language": "English",
"genres": [
"Action",
"Adventure",
"Science-Fiction"
],
"status": "Ended",
"runtime": 60,
"premiered": "1990-09-20",
"officialSite": null,
"schedule": {
"time": "20:30",
"days": [
]
},
"rating": {
"average": 8.6
},
"weight": 71,
"network": {
"id": 2,
"name": "CBS",
"country": {
"name": "United States",
"code": "US",
"timezone": "America/New_York"
}
},
"webChannel": null,
"externals": {
"tvrage": 5781,
"thetvdb": 78650,
"imdb": "tt0098798"
},
"image": {
"medium": "http://static.tvmaze.com/uploads/images/medium_portrait/4/11360.jpg",
"original": "http://static.tvmaze.com/uploads/images/original_untouched/4/11360.jpg"
},
"summary": "<p>The Flash is Barry Allen, a police scientist, who was working one night during a terrible thunder storm when a bolt of lightning crashes through the lab window, electrocuting him. Barry survives and soon learns that he is now able to move at almost incomprehensible speed.</p>",
"updated": 1509496066,
"_links": {
"self": {
"href": "http://api.tvmaze.com/shows/528"
},
"previousepisode": {
"href": "http://api.tvmaze.com/episodes/47947"
}
}
}
},
主要活动
package com.vineet.moviesapp;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "LOG MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnGet = findViewById(R.id.btnGet);
btnGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateTextView();
}
});
}
public void updateTextView(){
NetworkTask networkTask = new NetworkTask();
networkTask.execute("https://api.tvmaze.com/search/shows?q=flash");
}
class NetworkTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... strings) {
String stringUrl = strings[0];
try {
URL url = new URL(stringUrl);
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
Scanner scanner = new Scanner(inputStream);
scanner.useDelimiter("\\A");
if (scanner.hasNext()){
String s = scanner.next();
return s;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
ArrayList<Movie> movieArrayList = parseJson(s);
Log.d(TAG,"onPostExec"+ movieArrayList.size());
}
}
ArrayList<Movie> parseJson(String s){
ArrayList<Movie> movies = new ArrayList<>();
try {
JSONArray root = new JSONArray(s);
for(int i=0; i<root.length(); i++){
JSONObject object = root.getJSONObject(i);
Integer score = object.getInt("score");
String name = object.getJSONObject("show").getString("name");
String language = object.getJSONObject("show").getString("language");
String image = object.getJSONObject("show").getJSONObject("image").getString("medium");
Double rating = object.getJSONObject("show").getDouble("average");
Movie movie = new Movie(score,name,language,image,rating);
movies.add(movie);
}
} catch (JSONException e) {
e.printStackTrace();
}
return movies;
}
}
我尝试过以我的方式解析数据。但无法解析它。请帮助我,让我知道我在哪里犯了错误。
【问题讨论】:
-
您发布的 json 缺少尾随
]而不是逗号。在您的应用程序中也是如此吗? -
没有。我刚刚发布了一些来自 api 的对象,为什么你看不到 ]
标签: android json api networking