我尝试从API的内容中获取报价和作者。因为我只想在Android App中显示报价和作者。
{
"success": {
"total": 1
},
"contents": {
"quotes": [
{
"quote": "Give me golf clubs, fresh air and a beautiful partner, and you can keep the clubs and the fresh air.",
"length": "100",
"author": "Jack Benny",
"tags": {
"0": "funny",
"1": "golf",
"2": "sports",
"4": "tod"
},
"category": "funny",
"language": "en",
"date": "2020-05-17",
"permalink": "https://theysaidso.com/quote/jack-benny-give-me-golf-clubs-fresh-air-and-a-beautiful-partner-and-you-can-keep",
"id": "CzZGhQUP5nApJRq5BaqiggeF",
"background": "https://theysaidso.com/img/qod/qod-funny.jpg",
"title": "Funny Quote of the day"
}
]
},
"baseurl": "https://theysaidso.com",
"copyright": {
"year": 2022,
"url": "https://theysaidso.com"
}
}
但是发生了错误。我试图将jsonObj.getJSONArray(“ contents”)更改为jsonObj.getJSONArray(“ quotes”)。但是,它也有一个错误,即引号中不返回任何值。
org.json.JSONException: Value {"quotes":[{"quote":"Give me golf clubs, fresh air and a beautiful partner, and you can keep the clubs and the fresh air.","length":"100","author":"Jack Benny","tags":{"0":"funny","1":"golf","2":"sports","4":"tod"},"category":"funny","language":"en","date":"2020-05-17","permalink":"https:\/\/theysaidso.com\/quote\/jack-benny-give-me-golf-clubs-fresh-air-and-a-beautiful-partner-and-you-can-keep","id":"CzZGhQUP5nApJRq5BaqiggeF","background":"https:\/\/theysaidso.com\/img\/qod\/qod-funny.jpg","title":"Funny Quote of the day"}]} at contents of type org.json.JSONObject cannot be converted to JSONArray
我如何只获得报价和作者?
这是我的编码。
public class MainActivity extends AppCompatActivity {
private TextView TextViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextViewResult = findViewById(R.id.view);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://quotes.rest/qod?category=funny&language=en")
.get()
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
if(response.isSuccessful()){
final String MyResponse = response.body().string();
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(MyResponse);
} catch (JSONException e) {
e.printStackTrace();
}
try {
assert jsonObj != null;
JSONArray contacts = jsonObj.getJSONArray("contents");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(1);
final String id = c.getString("id");
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
TextViewResult.setText(id);
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
//
// MainActivity.this.runOnUiThread(new Runnable() {
// @Override
// public void run() {
//
// TextViewResult.setText(MyResponse);
// }
// });
}else {
throw new IOException("Unexpected code " + response);
};
}
});
}
}
contents
is an Object not an Array remember all JSON array have[
and]
at start and end in there JSON representation.Any thing that starts with
{
and}
is a struct/JSONObject for JSON parsers您的for循环可以如下修复