如何将JSON中的字段解析为String数组

时间:2016-10-16 08:07:55

标签: android json

这是我的JSON

[{
    "album_id": "71",
    "user_id": "38",
    "category_name": "Gallery 1",
    "album_image": "../images/album/gallery1rapport3871.jpg",
    "org_image_name": "Love-Couple-3.jpg",
    "date": "16-10-2016",
    "status": "0"
}, {
    "album_id": "73",
    "user_id": "38",
    "category_name": "Gallery 1",
    "album_image": "../images/album/gallery1rapport3873.jpeg",
    "org_image_name": "t1.daumcdn.net.jpeg",
    "date": "16-10-2016",
    "status": "0"
}]

我想要获取所有org_image_name值。但是当我使用Toast显示它时,我只获得一个值。

这是我的代码。请帮帮我。

public class Album_Display extends FragmentActivity {
    GridView grid1;
    CustomGrid_Album adapter;
    private ProgressDialog pDialog;
    String Uid,Disp;
    public String category;
    public String selected;
    public static String imagename;
    Button Alb_sel;
    public  static TextView cart_count,disp;
    ArrayList<Item_album> gridArray = new ArrayList<Item_album>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.album_display);
        grid1 = (GridView) findViewById(R.id.gridView2);
        Uid = getIntent().getStringExtra("id");
        Disp = getIntent().getStringExtra("disp");
        disp = (TextView) findViewById(R.id.top_Album_text);
        disp.setText(Disp);
        Datas.Uid=Uid;
        Alb_sel=(Button)findViewById(R.id.album_to_select);

        pDialog = new ProgressDialog(Album_Display.this);
        pDialog.setMessage("Loading...");
        pDialog.show();

        final RequestQueue queue =        Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_Gallery1,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Datas.imageIds = new String[response.length()];
                        JSONArray arr = null;


                        try {
                            arr = new JSONArray(response);
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                        int i=0;

                            for (i = 0; i < arr.length(); i++) {
                                try {

                                JSONObject obj = arr.getJSONObject(i);
                                    category = obj.getString("category_name");
                                    selected = obj.getString("album_id");
                                    imagename = obj.getString("org_image_name");

                                Datas.imageIds[i] = AppConfig.URL_IMAGE_temp+obj.getString("album_image").substring(3);

                                gridArray.add(new Item_album(Datas.imageIds[i]));

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

                        final int xl = i;
                        adapter = new CustomGrid_Album(Album_Display.this,xl,gridArray);
                        adapter.notifyDataSetChanged();
                        grid1.setAdapter(adapter);
                        pDialog.dismiss();
                        grid1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                                Intent intent = new Intent(Album_Display.this, Display.class);
                                intent.putExtra("count", xl);
                                intent.putExtra("pos", position);
                                startActivity(intent);
                            }
                        });
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {


            }

        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("id", Uid);
                return params;
            }

        };

        queue.add(stringRequest);
    }


    public void  back(View view){
        finish();
    }

    public void upload_img(View view){
        Intent in = new Intent(Album_Display.this, Image_Upload.class);
        startActivity(in);
    }

    public void select(View view){

        Toast.makeText(getApplication(), imagename, Toast.LENGTH_LONG).show();

       Intent in=new Intent(Album_Display.this,Album_Select.class);
       in.putExtra("category_name", category);
       in.putExtra("album_id", selected);
       in.putExtra("org_image_name", imagename);
       startActivity(in);
    }

}

1 个答案:

答案 0 :(得分:0)

查看"org_image_name"属性的值。 JSON响应本身返回String值而不是Array。首先,我认为它应该代表如下的响应,

"org_image_name":["imgOne.jpg", "imgTwojpg"],
"date": "16-10-2016",
"status": "0"

然后,您需要将其解析为String数组。