如何避免java中针对java.util.HashMap的未检查调用警告
我将图像存储为字符串,并将其转换为JSONObject,然后将其存储在json文件中,最后从同一个json文件中读取,并将字符串作为图像检索。在此过程中,我收到一条警告 Simple.java:29:警告:[未选中]未选中调用put(K,V)作为原始类型java.util.HashMap的成员 put(“imagestring”,base64String); ^ 1警告 代码是:如何避免java中针对java.util.HashMap的未检查调用警告,java,json,Java,Json,我将图像存储为字符串,并将其转换为JSONObject,然后将其存储在json文件中,最后从同一个json文件中读取,并将字符串作为图像检索。在此过程中,我收到一条警告 Simple.java:29:警告:[未选中]未选中调用put(K,V)作为原始类型java.util.HashMap的成员 put(“imagestring”,base64String); ^ 1警告 代码是: 您不能像JSONObject那样使用原始类型:公共类JSONObject扩展HashMap实现Map、JSONAw
您不能像JSONObject
那样使用原始类型:公共类JSONObject扩展HashMap实现Map、JSONAware、jsonstreamware{
您可能需要一个比org.JSON更好的JSON API来创建JSON;例如Jackson。您可以使用JSONObject的泛型(如果可用)
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.json.simple.JSONObject;
import java.io.FileWriter;
import java.io.FileReader;
import javax.xml.bind.DatatypeConverter;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Simple
{
public static void main(String[] args) throws IOException
{
JSONParser jsonParser=new JSONParser();
ByteArrayOutputStream baos=new ByteArrayOutputStream(1000);
BufferedImage img=ImageIO.read(new File("/root/Desktop","Screenshot.png"));
ImageIO.write(img, "png", baos);
baos.flush();
//to store the image as a string
String base64String=DatatypeConverter.printBase64Binary(baos.toByteArray());
baos.close();
System.out.println("string"+base64String);
JSONObject fjson=new JSONObject();
fjson.put("imagestring",base64String);
//the string is written into a json file
try
{
FileWriter writer=new FileWriter("/root/project/code/json/res.json");
writer.write(fjson.toString());
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
//reading from a json file
try
{
FileReader fr=new FileReader("/root/project/code/json/res.json");
try
{
JSONObject jParse=(JSONObject)jsonParser.parse(fr);
String jsonString=(String)jParse.get("imagestring");
byte[] bytearray =DatatypeConverter.parseBase64Binary(jsonString);
BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytearray));
ImageIO.write(imag, "png", new File("/root/project/code/json","snap3.png"));
}
catch(ParseException e)
{
e.printStackTrace();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}