如何在 AS 中解析 JSON 数据
How to parse JSON data in AS
问题:
我正在从 JSON format.I 中的 Web 服务获取数据,我正在尝试解析 "quote" 和 "Author",但是,我做不到。
这里是数据:
{
"success": {
"total": 1
},
"contents": {
"quotes": [
{
"quote": "You are very powerful, provided you know how powerful you are.",
"author": "Yogi Bhajan",
"category": "inspire",
"date": "2018-02-15",
"title": "Inspiring Quote of the day"
}
],
"copyright": "2017-19 theysaidso.com"
}
}
我的 Java 代码是:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("quotes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject getQuote = jsonArray.getJSONObject(i);
String quoteOfTheDay = getQuote.getString("quote");
String author = getQuote.getString("author");
quotesView.append(quoteOfTheDay + author + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
我确定 Java class 中的解析逻辑不正确。
我应该使用哪种逻辑来纠正这个问题?
首先,在提取数组 quotes
之前,您必须先获取 JSONObject
contents
。
try {
JSONObject obj = response.getJSONObject("contents"); //first get the JSONObject contents
JSONArray jsonArray = obj.getJSONArray("quotes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject getQuote = jsonArray.getJSONObject(i);
String quoteOfTheDay = getQuote.getString("quote");
String author = getQuote.getString("author");
quotesView.append(quoteOfTheDay + author + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
您错过了参赛者 JSONObject。
复制粘贴下面的代码:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject contentsObject=response.getJSONObject("contents");
JSONArray jsonArray = contentsObject.getJSONArray("quotes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject getQuote = jsonArray.getJSONObject(i);
String quoteOfTheDay = getQuote.getString("quote");
String author = getQuote.getString("author");
quotesView.append(quoteOfTheDay + author + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject contents =
response.getJsonObject("contents");
try {
JSONArray quotes = contents.getJSONArray("quotes");
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < quotes.length(); i++) {
JSONObject getQuote = jsonArray.getJSONObject(i);
String quoteOfTheDay = getQuote.getString("quote");
String author = getQuote.getString("author");
quotesView.append(quoteOfTheDay + author + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
试试这个...
问题:
我正在从 JSON format.I 中的 Web 服务获取数据,我正在尝试解析 "quote" 和 "Author",但是,我做不到。
这里是数据:
{
"success": {
"total": 1
},
"contents": {
"quotes": [
{
"quote": "You are very powerful, provided you know how powerful you are.",
"author": "Yogi Bhajan",
"category": "inspire",
"date": "2018-02-15",
"title": "Inspiring Quote of the day"
}
],
"copyright": "2017-19 theysaidso.com"
}
}
我的 Java 代码是:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("quotes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject getQuote = jsonArray.getJSONObject(i);
String quoteOfTheDay = getQuote.getString("quote");
String author = getQuote.getString("author");
quotesView.append(quoteOfTheDay + author + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
我确定 Java class 中的解析逻辑不正确。
我应该使用哪种逻辑来纠正这个问题?
首先,在提取数组 quotes
之前,您必须先获取 JSONObject
contents
。
try {
JSONObject obj = response.getJSONObject("contents"); //first get the JSONObject contents
JSONArray jsonArray = obj.getJSONArray("quotes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject getQuote = jsonArray.getJSONObject(i);
String quoteOfTheDay = getQuote.getString("quote");
String author = getQuote.getString("author");
quotesView.append(quoteOfTheDay + author + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
您错过了参赛者 JSONObject。
复制粘贴下面的代码:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject contentsObject=response.getJSONObject("contents");
JSONArray jsonArray = contentsObject.getJSONArray("quotes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject getQuote = jsonArray.getJSONObject(i);
String quoteOfTheDay = getQuote.getString("quote");
String author = getQuote.getString("author");
quotesView.append(quoteOfTheDay + author + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject contents =
response.getJsonObject("contents");
try {
JSONArray quotes = contents.getJSONArray("quotes");
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < quotes.length(); i++) {
JSONObject getQuote = jsonArray.getJSONObject(i);
String quoteOfTheDay = getQuote.getString("quote");
String author = getQuote.getString("author");
quotesView.append(quoteOfTheDay + author + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
试试这个...