Android 如何到达图像节点
我有一个JSON结构,如下所示,我无法以任何方式到达图像节点。我怎样才能用截击库做到这一点?任何想法都会有帮助。提前谢谢Android 如何到达图像节点,android,json,android-volley,Android,Json,Android Volley,我有一个JSON结构,如下所示,我无法以任何方式到达图像节点。我怎样才能用截击库做到这一点?任何想法都会有帮助。提前谢谢 { "nodes": [ { "node": { "id": "2536", "title": "Die Düsseldorfer Rabbiner", "image": { "src": "how to reach here", "alt": "",
{
"nodes": [
{
"node": {
"id": "2536",
"title": "Die Düsseldorfer Rabbiner",
"image": {
"src": "how to reach here",
"alt": "",
"title": "© Landeshauptstadt Düsseldorf/Michael Gstettenbauer"
},
"body": "some text",
"category": "Deutsch",
"created": "1481344134",
"url": "some text"
}
}
]
}
代码块
JsonArrayRequest getRequest = new JsonArrayRequest(
url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
JSONObject jsonObject = new JSONObject("response");
JSONArray jsonArray = jsonObject.getJSONArray("nodes");
for(int i = 0; i<jsonArray.length(); i++){
JSONObject jo = jsonArray .getJSONObject(i);
String name = jo.getString("category");
Toast.makeText(context, name, Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
JsonArrayRequest getRequest=newjsonarrayrequest(
网址,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
试一试{
JSONObject JSONObject=新的JSONObject(“响应”);
JSONArray JSONArray=jsonObject.getJSONArray(“节点”);
对于(int i=0;i您的Java模型应该与JSON完全匹配。JSON模式中的基本元素是对象,而不是数组;因此您的请求应该是JSONObjectRequest
,而不是JSONArrayRequest
:
JSONObjectRequest request = new JSONObjectRequest(url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONArray response) {
try {
JSONArray nodes = response.getJSONArray("nodes");
for(int i = 0; i < nodes.length; i++) {
JSONObject obj = nodes.getJSONObject(i);
JSONObject node = obj.getJSONObject("node");
JSONObject image = node.getJSONObject("image");
String title = image.getString("title");
Toast.makeText(context, title, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
JSONObjectRequest=新的JSONObjectRequest(url,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
试一试{
JSONArray nodes=response.getJSONArray(“节点”);
对于(int i=0;i
正如您可能看到的,如果您有一个大的数据集,这可能会变得单调乏味。这就是为什么我强烈建议使用JSON解析库;例如,或。有一个很好的教程,介绍如何使用中的这些库发出自定义请求。您的Java模型应该与JSON完全匹配。JS中的基本元素架构上的是对象,而不是数组;因此您的请求应该是JSONObjectRequest
,而不是JSONArrayRequest
:
JSONObjectRequest request = new JSONObjectRequest(url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONArray response) {
try {
JSONArray nodes = response.getJSONArray("nodes");
for(int i = 0; i < nodes.length; i++) {
JSONObject obj = nodes.getJSONObject(i);
JSONObject node = obj.getJSONObject("node");
JSONObject image = node.getJSONObject("image");
String title = image.getString("title");
Toast.makeText(context, title, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
JSONObjectRequest=新的JSONObjectRequest(url,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
试一试{
JSONArray nodes=response.getJSONArray(“节点”);
对于(int i=0;i
正如您可能看到的,如果您有一个大的数据集,这可能会变得单调乏味。这就是为什么我强烈建议使用JSON解析库;例如,或。有一个很好的教程,介绍如何通过截取来定制请求,以便在中使用这些库。从外观上看,您应该使用JsonObjectRequest而不是JSONArrayRequest。
JsonObjectRequest=newJSONObjectRequest(url,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray数组=response.getJSONArray(“节点”);
对于(inti=0;i,从外观上看,应该使用JsonObjectRequest而不是JsonArrayRequest。
JsonObjectRequest=newJSONObjectRequest(url,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray数组=response.getJSONArray(“节点”);
对于(int i=0;i)您是如何解析json的,手动解析还是使用任何库(如-GSON)。new-JSONObject(“object”)
您认为它有什么作用?嗨,Ravinder,我已经添加了我的代码块。您能检查一下吗?我认为我做得不错,但它不起作用。@njzk2抱歉,我认为它应该是“response”不应该吗?您是如何解析json的,手动解析还是使用任何类似-GSON的库。new JSONObject(“object”)
您认为它有什么作用?嗨,Ravinder,我已经添加了我的代码块。您能检查一下吗?我想我做得对,但它不起作用。@njzk2对不起,我想应该是“响应”吧?