我的输入就是那样
JSONArray中的所有对象都具有相同的数据。 JSONArray包含运行循环的soma数据我认为它将最后一层的数据复制到每个JSONObject
{"layers":[{"flag":"40","name":"Layer `40","opacity":"-1","bottom":"386","left":"394","right":"410","mode":"layer_mode","top":"370"},{"flag":"40","name":"Layer` 40","opacity":"-1","bottom":"386","left":"394","right":"410","mode":"layer_mode","top":"370"}],"header":{"height":"690","width":"500","color_mode":"3","channels":"3","depth":null,"signature":"8BPS","version":"1"}}
所需的输出
我想要的输出看起来也是这样,但JSONArray中每个JSONObject中的数据应该是唯一的或者彼此不同 我的代码就是那样
package psd;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import psd.layer.PSDLayerPixelData;
import psd.layer.PSDLayerStructure;
import psd.section.PSDLayerAndMask;
import psd.section.PSDHeader;
import org.json.simple.*;
public class TestPSD {
List<PSDLayerStructure> layers = new ArrayList<PSDLayerStructure>();
List<PSDLayerPixelData> images = new ArrayList<PSDLayerPixelData>();
String signature,color_mode,column,rows,depth,channels,version,img_res,Header,heading,layer_name,layer_top,layer_right,layer_bottom,layer_left,layer_opacity,layer_mode,layer_flag,layer_channel;
JSONObject header = new JSONObject() ;
JSONObject full_info = new JSONObject();
JSONObject layer_info = new JSONObject();
JSONArray layer_array = new JSONArray();
JSONObject total_layers = new JSONObject();
int count=0;
@SuppressWarnings("unchecked")
public void parser(File f) throws IOException {
PSDParser parser = new PSDParser(new FileInputStream(f));
PSDLayerAndMask layerAndMask = parser.getLayerAndMask();
signature =String.valueOf( parser.getHeader().getSignature());
color_mode= String.valueOf(parser.getHeader().getMode());
column =String.valueOf(parser.getHeader().getColumns());
rows = String.valueOf(parser.getHeader().getRows());
channels = String.valueOf(parser.getHeader().getChannel());
version = String.valueOf(parser.getHeader().getVersion());
header.put("signature", signature);
header.put("color_mode", color_mode );
header.put("height", column);
header.put("width", rows);
header.put("depth", depth);
header.put("channels", channels);
header.put("version", version);
//System.out.println(full_header);
//Header = parser.getHeader().toString();
//full_header.put("header",String.valueOf(Header));
img_res=parser.getImageResources().toString();
layers = layerAndMask.getLayers();
images = layerAndMask.getImageLayers();
int i = 0;
//int j=1;
for (PSDLayerStructure layer : layers) {
PSDLayerPixelData pixelData = images.get(i);
BufferedImage image = pixelData.getImage();
if (image != null){
layer_name = String.valueOf(layer.getName());
layer_bottom = String.valueOf(layer.getBounds().getBottom());
layer_left = String.valueOf(layer.getBounds().getLeft());
layer_right = String.valueOf(layer.getBounds().getRight());
layer_top = String.valueOf(layer.getBounds().getTop());
layer_opacity = String.valueOf(layer.getOpacity());
layer_mode = String.valueOf(layer.getBlendModeKey());
layer_flag = String.valueOf(layer.getFlags());
layer_info.put("name", layer_name);
layer_info.put("bottom", layer_bottom);
layer_info.put("left", layer_left);
layer_info.put("right", layer_right);
layer_info.put("top", layer_top);
layer_info.put("opacity", layer_opacity);
layer_info.put("mode","layer_mode");
layer_info.put("flag", layer_flag);
layer_array.add(layer_info);
ImageIO.write(image, "png", new File("Images/"+layer.getName() + ".png"));
}
i++;
count++;
}
full_info.put("layers", layer_array);
full_info.put("header",header);
try {
FileWriter file = new FileWriter("C:\\Users\\Faizan\\workspace\\psd_to_html\\psd_to_html\\psd.json");
file.write(full_info.toString());
file.flush();
file.close();
System.out.println("created");
//break;
} catch (IOException e) {
e.printStackTrace();
}
}
// public JSONObject getarray()
// {
// return layer_info;
// }
public JSONObject layers()
{
return total_layers;
}
public JSONArray getlayers()
{
return layer_array;
}
public JSONObject getlayer()
{
return layer_info;
}
// public String getheading()
// {
// return heading;
// }
// public String getresoucre()
// {
// return img_res;
// }
public JSONObject getinfo()
{
return full_info ;
}
public int count()
{
return count;
}
// public String getheader()
// {
// return Header;
// }
}