New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fastjson转对象失败 #302

Closed
michael8335 opened this Issue Mar 2, 2015 · 4 comments

Comments

Projects
None yet
5 participants
@michael8335
Copy link

michael8335 commented Mar 2, 2015

版本1.1.41
代码:
public static void main(String agrs[])
{

    BannerContent content=new Banner().new BannerContent();
    ImageURL url=new ImageUtils.ImageURL();
    url.setHeight(150);
    url.setWidth(30);
    url.setUrl("T1ZtJTByVT1RCvBVdK.jpg");
    content.setContentCover(url);
    content.setContentID(50);
    content.setContentType(1);
    content.setTitle("test");
    System.out.println(JsonUtils.toJson(content));
    JsonUtils.parseObject(
            JsonUtils.toJson(content), Banner.BannerContent.class);
}

其中
public class Banner {
private int id;
private String content;
private int isAlive;
private int priority;
private String cover;
private BannerContent bannerContent;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getContent() {

    return this.content;
}

public void setContent(String content) {
    this.content = content;
}

public int getIsAlive() {
    return isAlive;
}

public void setIsAlive(int isAlive) {
    this.isAlive = isAlive;
}

public BannerContent getBannerContent() {
    return this.bannerContent;
}

public void setBannerContent(BannerContent bannerContent) {
    this.bannerContent = bannerContent;
}

public int getPriority() {
    return priority;
}

public void setPriority(int priority) {
    this.priority = priority;
}

public String getCover() {
    return cover;
}

public void setCover(String cover) {
    this.cover = cover;
}

public class BannerContent implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = -7749989258574004161L;
    private int contentID;
    private int contentType;
    private ImageUtils.ImageURL contentCover;
    private String title;

    public int getContentID() {
        return contentID;
    }

    public void setContentID(int contentID) {
        this.contentID = contentID;
    }

    public int getContentType() {
        return contentType;
    }

    public void setContentType(int contentType) {
        this.contentType = contentType;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public ImageUtils.ImageURL getContentCover() {
        return contentCover;
    }

    public void setContentCover(ImageUtils.ImageURL contentCover) {
        this.contentCover = contentCover;
    }
}

}

public class ImageUtils {

public static String getImageURL(String json, String tfsPath) {
    try {
        ImageURL image = (com.lexue.common.utils.ImageUtils.ImageURL) JsonUtils
                .parseObject(json, ImageURL.class);
        return tfsPath + image.getUrl();
    } catch (Exception e) {
        return tfsPath + json;
    }
}

public static String getExtention(String fileName) {
    int pos = fileName.lastIndexOf(".");
    return fileName.substring(pos);
}

public static String getFormatURL(File image, String imageName)
        throws IOException {
    Image src = javax.imageio.ImageIO.read(image);
    int w = src.getWidth(null);
    int h = src.getHeight(null);
    return JsonUtils.toJson(new ImageURL(imageName, w, h));
}

public static class ImageURL {
    private String url;
    private int width;
    private int height;

    public ImageURL() {

    }

    public ImageURL(String url, int width, int height) {
        this.setUrl(url);
        this.setWidth(width);
        this.setHeight(height);
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

}

问题是对象转Json可以,但是Json转对象时,总报如下异常
Exception in thread "main" com.alibaba.fastjson.JSONException: create instance error, class com.lexue.banner.dao.entity.Banner$BannerContent
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.createInstance(JavaBeanDeserializer.java:100)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:303)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:119)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:514)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:244)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:220)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:179)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:327)
at com.lexue.common.utils.JsonUtils.parseObject(JsonUtils.java:23)
at com.lexue.common.utils.JsonUtils.main(JsonUtils.java:46)
Caused by: java.lang.NullPointerException
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.createInstance(JavaBeanDeserializer.java:97)
... 9 more

@yakolee

This comment has been minimized.

Copy link
Contributor

yakolee commented Mar 9, 2015

请使用新版本1.2.4

@gavinHuang

This comment has been minimized.

Copy link

gavinHuang commented Jul 24, 2015

@yakolee 新版本并不能解决问题。
我也有Michael 类似的问题,一样的代码,只要把内部类BannerContent单独成一个正常的类,问题就自动消失。

@wenshao

This comment has been minimized.

Copy link
Member

wenshao commented Apr 13, 2016

内部非静态类无法实例化,所以无法实现的,关闭issue啦,有问题再反馈

@wenshao wenshao closed this Apr 13, 2016

@duqicauc

This comment has been minimized.

Copy link

duqicauc commented Jul 3, 2017

学习了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment