Fastjson deserialize returns null

827 Views Asked by At

This is the json file:{"Type": "File", "File_Name": "tempfile.fasttext", "File_Size": 15} i use Fastjson to deserialize it and show the items..but

    public static void deserializejson(String json){
        fileinfo fileinfo2 = JSON.parseObject(json, fileinfo.class);
        System.out.println(fileinfo2.getfiletype());
        System.out.println(fileinfo2.getFile_Name());
        System.out.println(fileinfo2.getfilesize());
    }
public class fileinfo {
    private String File_Name;
    private String Type;
    private int File_Size;

    public String getFile_Name(){
        return  File_Name;
    }

    public String getfiletype(){
        return Type;
    }

    public int getfilesize(){
        return File_Size;
    }
}

but it didn't return what i expected , but

null
null
0

what is the problem of my code? plz forgive my bad english and Thanks again

2

There are 2 best solutions below

0
PatrickChen On

You need the setter function, JSON needs setter to inject the values when parsing the JSON string. And all your getters' format is not right. Try with this:

class fileinfo {
    private String File_Name;
    private String Type;
    private int File_Size;

     public String getFile_Name() {
         return File_Name;
     }

     public void setFile_Name(String file_Name) {
         File_Name = file_Name;
     }

     public String getType() {
         return Type;
     }

     public void setType(String type) {
         Type = type;
     }

     public int getFile_Size() {
         return File_Size;
     }

     public void setFile_Size(int file_Size) {
         File_Size = file_Size;
     }
 }

0
Bogojob On

I think your problem lies in the syntax you use to write the string json. If you use all lowercase letters both in the json string and in the variable declaration in your fileInfo class, the deserialization is done correctly. I used the Jackson library below is the working program.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


public class App {
static String jsondata = "{\"type\": \"file\", \"filename\": \"tempfile.fasttext\", \"filesize\": 15}";

public static void main(String[] args) {

    ObjectMapper mapper = new ObjectMapper();
    fileinfo fi = null;
    try {
        fi = mapper.readValue(jsondata, fileinfo.class);
    } catch (JsonMappingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("data drom json to infoFile.class =>" + fi.getFilename());

}

public static class fileinfo {
    private String filename;
    private String type;
    private int filesize;

    public fileinfo() {
        super();
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getFilesize() {
        return filesize;
    }

    public void setFilesize(int filesize) {
        this.filesize = filesize;
    }

}

}

output: data from json to infoFile.class =>tempfile.fasttext