Android 如何将JSON中的字段解析为字符串数组
这是我的Android 如何将JSON中的字段解析为字符串数组,android,json,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" }, { "al
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\u image\u 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);
}
}
public class相册\u显示扩展了碎片活动{
GridView grid1;
自定义网格相册适配器;
私人对话;
字符串Uid,Disp;
公共字符串类别;
选择公共字符串;
公共静态字符串imagename;
按钮Alb_选择;
公共静态文本视图购物车计数,显示;
ArrayList gridArray=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.album_显示);
grid1=(GridView)findViewById(R.id.gridView2);
Uid=getIntent().getStringExtra(“id”);
Disp=getIntent().getStringExtra(“Disp”);
disp=(TextView)findViewById(R.id.top\u相册\u text);
disp.setText(disp);
Datas.Uid=Uid;
Alb_sel=(按钮)findviewbyd(R.id.album_to_select);
pDialog=新建进度对话框(相册显示);
设置消息(“加载…”);
pDialog.show();
final RequestQueue=Volley.newRequestQueue(getApplicationContext());
StringRequest StringRequest=新的StringRequest(Request.Method.POST,AppConfig.URL\u Gallery1,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Datas.imageIds=新字符串[response.length()];
JSONArray arr=null;
试一试{
arr=新JSONArray(响应);
}捕获(JSONException e1){
e1.printStackTrace();
}
int i=0;
对于(i=0;i
查看“org\u image\u name”
属性的值。JSON响应本身返回的是字符串值而不是数组。首先我认为它应该代表如下的反应
"org_image_name":["imgOne.jpg", "imgTwojpg"],
"date": "16-10-2016",
"status": "0"
然后,需要将其解析为字符串数组 查看
“org\u image\u name”
属性的值。JSON响应本身返回的是字符串值而不是数组。首先我认为它应该代表如下的反应
"org_image_name":["imgOne.jpg", "imgTwojpg"],
"date": "16-10-2016",
"status": "0"
然后,需要将其解析为字符串数组 您应该为
JSON
创建一个模型类,并使用GSON
@Neeraj进行解析。我正在从JSON获取所有图像,但它由gridview填充。我想获取所有组织的图像名称。目前我只获取一个值。网格是否显示所有图像?如果是,则从gridArray
获取。是否需要数据。ImageID应具有以下内容?Datas.imageIds=[“Love-Couple-3.jpg”,“t1.daumdn.net.jpeg”]根据您的回答?@Neeraj所有图像我指的是“相册图像”对象。我想取一个像“相册图像”一样的组织图像名称。但是我不想展示它。目前,toast中只显示了一个“org_image_name”值。您应该为JSON
创建一个模型类,并使用GSON
@Neeraj进行解析。我正在从JSON中获取所有图像,但它由gridview填充。我想要