JSON解析Android的JSON数组和JSON对象 [英] JSON parsing android json arrays and json object

查看:109
本文介绍了JSON解析Android的JSON数组和JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到过凌空JSON响应,反应如下:

I'm getting a json response through volley, the response is as following :

{
   "status":"success",
   "message":"Request Successfull",
   "messagetitle":"Success",
   "show":"false",
   "goback":"false",
   "companies":[
      {
         "id":"14",
         "category":{
            "id":"1",
            "name":"test"
         },
         "subcategory":{
            "id":"1",
            "name":"test",
            "image":"https:\/\/page.com\/page\/uploads\/pagepics\/test\/testpics\/test.jpg"
         },
         "name2":"Company",
         "location":null,
         "logo":"https:\/\/page.com\/test\/testp\/testpics\/logo.png",
         "picture":"https:\/\/page.com\/test\/",
         "facebook":"https:\/\/www.facebook.com\/test",
         "instagram":"",
         "twitter":"",
         "telephone1":"+9611990454",
         "telephone2":"+961000000",
         "address":"Lebanon",
         "longitude":"0",
         "latitude":"0",
         "website":"www.website.com",
         "email":"",
         "desc":"asdasdas das das das dasda das das das dsadasdsadasdsad asd asd asd asd sad sa as das dsa asd das a a",
         "user":{
            "id":"21",
            "name":"X Y"
         },
         "status":"1"
      },
      {
         "id":"4",
         "category":{
            "id":"1",
            "name":"test"
         },
         "subcategory":{
            "id":"1",
            "name":"test",
            "image":"https:\/\/page.com\/test\/testp\/testpics\/test\/testphoto\/test.jpg"
         },
         "name2":"Your Company",
         "location":null,
         "logo":"",
         "picture":"https:\/\/page.com\/test\/",
         "facebook":"",
         "instagram":"",
         "twitter":"",
         "telephone1":"",
         "telephone2":"",
         "address":"",
         "longitude":"0",
         "latitude":"0",
         "website":"",
         "email":"",
         "desc":"",
         "user":{
            "id":"1",
            "name":"X Y"
         },
         "status":"1"
      }
   ]
}

我试图让所有的形象,从响应NAME2的对象。这是我的Java code:

I'm trying to get all "image" and "name2" objects from the response. Here is my java code:

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                link, null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
            //            Log.d("Request", response.toString());
                         try {

                              jarray1 = response.getJSONArray("subcategory");

for(int i=0;i<jarray1.length();i++)
{
    JSONObject object = (JSONObject) jarray1.get(i);



     String url = object.getString("image");
     String name= object.getString("name2");
    Toast.makeText(getBaseContext(),url+"\n"+name,Toast.LENGTH_LONG).show();
    }



                        } catch (JSONException e) {
                            e.printStackTrace();

                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }

那么它给了我错了信息,任何帮助吗?

Well it's giving me wrong info, any help please ?

推荐答案

您无法直接访问JSON对象内,根据您的JSON格式,你必须先获得公司JSON数组

You can't directly access inner objects in JSON, according to your json format you have to first get JSON Array of companies

JSONArray companiesArray = response.getJSONArray("companies");

然后得到的数组中的元素

Then get elements of the array

for(int i=0;i<companiesArray .length();i++){
    JSONObject object = (JSONObject) companiesArray.get(i);
    // Now get subcategory information as JSONObject 
    JSONObject subcategoryObject =object .getJSONObject("subcategory");
    // Now you can get image and name from subcategoryObject 
    String url = subcategoryObject.getString("image");
    String name= subcategoryObject.getString("name2");
}

这篇关于JSON解析Android的JSON数组和JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆