以下是修改后的代码:

JSONObject json = new JSONObject(jsonstr);
JSONArray data = json.getJSONArray("data");
int length = data.length();
String[] filenamesWithPath = new String[1000];			

for (int i = 0; i < length; i++) {
    r = sheet.createRow(i + rowcount);
    r.setHeight((short) (256 * 2));

    int cellNum = 0;
    JSONObject temp = data.getJSONObject(i);
    for (cellNum = 0; cellNum < displayKeys.length; cellNum++) {
    	
        String value = temp.optString(dataKeys[cellNum]);

        if (dataKeys[cellNum].equals("images")) {
            JSONArray images = temp.optJSONArray("images");
            if ((images != null)&&(cellNum<(displayKeys.length+images.length()-1))) {
                for (int j = 0; j < images.length(); j++) {
                	String imageUrl = images.optString(j) + "?imageMogr2/thumbnail/!20p";
                	String imageURLName = imageUrl.split("/")[-1];
                    
                    String imageHyperLinkString = "=HYPERLINK(\"" + imageUrl + "\")";
                    System.out.println(imageUrl);
                    System.out.println(imageHyperLinkString);
                    CellUtil.createCell(r, cellNum+j, imageHyperLinkString);
                    // 将imageUrl添加到allImageUrls中
                    filenamesWithPath[i] = imageURLName ;
                }
            }
        } else {
            CellUtil.createCell(r, cellNum, value);
        }
        System.out.println(filenamesWithPath);
    }
}

主要的修改包括:

  1. JSONObject temp = new JSONObject(data.optString(i)); 修改为 JSONObject temp = data.getJSONObject(i);,因为dataJSONArray类型,直接使用getJSONObject(i)获取对应的JSONObject对象即可。
  2. filenamesWithPath += imageURLName; 修改为 filenamesWithPath[i] = imageURLName;,因为filenamesWithPath是一个String数组,使用索引i来添加每个imageURLName的值
JSONObject json = new JSONObjectjsonstr;			Systemoutprintlnjson;			JSONArray data = jsongetJSONArraydata;			int length = datalength;			String filenamesWithPath = new String1000;									for int i = 0;

原文地址: https://www.cveoy.top/t/topic/iZsH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录