用 volley 解析嵌套 json
parse nested json with volley
我想像这样嵌套解析 json json
但我以前不使用嵌套 json,我知道单独解析 json 对象和数组,但我不知道嵌套 json 解析。我想在 posts
中获取 title
并在 tumbnail_images
中获取缩略图 url
我写了一些代码但是我有一个错误
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
final String posts = response.getString("posts");
// ================================================ Json Array Request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
String arrayResponse = "";
try {
for (int i = 0; i < response.length(); i++) {
JSONObject post = (JSONObject) response.get(i);
String title = post.getString("title");
arrayResponse += "title : " + title;
}
}
catch (JSONException e) {
e.printStackTrace();
}
//btnShow ClickListener
btnShowPost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtShowPosts.setText(arrayResponse);
}
});
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "We have a problem in Array Request ", Toast.LENGTH_SHORT).show();
}
});
jsonResponse = "";
jsonResponse += "posts " + posts;
);
}
catch(
JSONException e
)
{
e.printStackTrace();
Toast.makeText(MainActivity.this, "We have a problem ", Toast.LENGTH_SHORT).show();
}
}
}
,new Response.ErrorListener()
{
@Override
public void onErrorResponse (VolleyError error){
Toast.makeText(MainActivity.this, "We have a problem in volley ", Toast.LENGTH_SHORT).show();
}
}
);
AppController.getInstance().addToRequestQueue(jsonObjectRequest);
传递您的 json String ,您将在这里获得所有标题。
public ArrayList<String> getTitles(final JSONObject object){
ArrayList<String> returnData = new ArrayList<>();
try{
JSONArray postArray = object.getJSONArray("posts");
for(int x=0;x<postArray.length();x++){
returnData.add(postArray.getJSONObject(x).getString("title").toString());
}
}
catch (JSONException e){}
return returnData;
}
如何: 在您的 JSON 字符串中,您必须以这种方式解析标题。
- 1st 将字符串转换为 JSON。
- 解析 post
的数组
- 然后 运行 一个循环来获取 child 个元素。 (在这里你会找到所有的标题)
我想像这样嵌套解析 json json
但我以前不使用嵌套 json,我知道单独解析 json 对象和数组,但我不知道嵌套 json 解析。我想在 posts
中获取 title
并在 tumbnail_images
我写了一些代码但是我有一个错误
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
final String posts = response.getString("posts");
// ================================================ Json Array Request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
String arrayResponse = "";
try {
for (int i = 0; i < response.length(); i++) {
JSONObject post = (JSONObject) response.get(i);
String title = post.getString("title");
arrayResponse += "title : " + title;
}
}
catch (JSONException e) {
e.printStackTrace();
}
//btnShow ClickListener
btnShowPost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtShowPosts.setText(arrayResponse);
}
});
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "We have a problem in Array Request ", Toast.LENGTH_SHORT).show();
}
});
jsonResponse = "";
jsonResponse += "posts " + posts;
);
}
catch(
JSONException e
)
{
e.printStackTrace();
Toast.makeText(MainActivity.this, "We have a problem ", Toast.LENGTH_SHORT).show();
}
}
}
,new Response.ErrorListener()
{
@Override
public void onErrorResponse (VolleyError error){
Toast.makeText(MainActivity.this, "We have a problem in volley ", Toast.LENGTH_SHORT).show();
}
}
);
AppController.getInstance().addToRequestQueue(jsonObjectRequest);
传递您的 json String ,您将在这里获得所有标题。
public ArrayList<String> getTitles(final JSONObject object){
ArrayList<String> returnData = new ArrayList<>();
try{
JSONArray postArray = object.getJSONArray("posts");
for(int x=0;x<postArray.length();x++){
returnData.add(postArray.getJSONObject(x).getString("title").toString());
}
}
catch (JSONException e){}
return returnData;
}
如何: 在您的 JSON 字符串中,您必须以这种方式解析标题。
- 1st 将字符串转换为 JSON。
- 解析 post 的数组
- 然后 运行 一个循环来获取 child 个元素。 (在这里你会找到所有的标题)