您的位置:首页 > Web前端 > JQuery

IE parseerror with JSON&jQuery

2013-08-29 13:02 423 查看
Recently my project has a page which will load amount of data, as this project use jQuery heavily even use it in wrong way, say return data from DB and then java layer only wraps it to Object, with JBoss Resteasy, this Object is transferred to javascript
layer as JSON type.

After this, big challenge comes to Browser, JS would iterate this complex Object to get demanded data, this process involves loop, filtering and sorting, even second level loop for filter duplicated data. The time to this process is as long as IE Browser
complaints with

Error message: "A script on this page is causing Internet Explorer to run slowly"


Under this circumstance, we would like to withdraw data handling work from JS to Java, we define below java data structure:

public class TurbineAuxiliary {
private List<Customer> customers = new ArrayList<TurbineAuxiliary.Customer>();
private List<Site> sites = new ArrayList<TurbineAuxiliary.Site>();
private List<Country> countries = new ArrayList<TurbineAuxiliary.Country>();
private List<SiteTurbine> siteTurbines = new ArrayList<TurbineAuxiliary.SiteTurbine>();
private List<UnitAttr> unitAttrs = new ArrayList<TurbineAuxiliary.UnitAttr>();
private List<Technology> techs = new ArrayList<TurbineAuxiliary.Technology>();
private Set<String> regions = new LinkedHashSet<String>();
private Set<String> outageTypes = new LinkedHashSet<String>();
private Set<String> contractRaxs = new LinkedHashSet<String>();
private Set<String> csaTriggers = new LinkedHashSet<String>();


However, after JSON transform, I got below JSON data structure (even some fields are lost), it is very strange:



Anyway, if no data is lost, then corrupted data structure would not block data extracting. This data structure works fine in Chrome, but failed in IE, IE just reports 

status: 200
textstatus:parsererror
errorThrown: Expected '}'


I tried to search Internet in the hope of getting some hint or solution on this, several feasible solutions for others come to me, but no luck with tries. like:

USING JSON IN INTERNET EXPLORER WITH JQUERY
IE8 + Jquery ajax call giving parsererror from dJango : for json data which seems valid
in Firefox

My colleague change another idea, as he doubts JAXB would not recognize (or incompatible with ) Java Set, consequently the generic String cannto be parsed correctly, so he tried to use List instead of Set as well as String wrapping. The updated Java data
structure is as following:

@XmlRootElement
public class TurbineAuxiliary {
private List<Customer> customers = new ArrayList<TurbineAuxiliary.Customer>();
private List<Site> sites = new ArrayList<TurbineAuxiliary.Site>();
private List<Country> countries = new ArrayList<TurbineAuxiliary.Country>();
private List<SiteTurbine> siteTurbines = new ArrayList<TurbineAuxiliary.SiteTurbine>();
private List<UnitAttr> unitAttrs = new ArrayList<TurbineAuxiliary.UnitAttr>();
private List<Technology> techs = new ArrayList<TurbineAuxiliary.Technology>();
private List<StringWrapperForJAXB> regions = new ArrayList<StringWrapperForJAXB>();
private List<StringWrapperForJAXB> outageTypes = new ArrayList<StringWrapperForJAXB>();
private List<StringWrapperForJAXB> contractRaxs = new ArrayList<StringWrapperForJAXB>();
private List<StringWrapperForJAXB> csaTriggers = new ArrayList<StringWrapperForJAXB>();
public static class StringWrapperForJAXB implements Comparable<StringWrapperForJAXB>{
private String core;

public StringWrapperForJAXB() {
}

public StringWrapperForJAXB(String core) {
super();
this.core = core;
}

@Override
public int compareTo(StringWrapperForJAXB o) {
return this.core.compareTo(o.getCore());
}

public String getCore() {
return core;
}

public void setCore(String core) {
this.core = core;
}
}


After this change, we are getting correct JSON successfully and no data is lost, see:



From my experience, I found 2 points needs to be remarked:

java.util.Set is not compatible with JAXB
When use java generic, direclty use List<String> cannot be parsed correctly by JAXB, but need to have a wrapper for String.

Not have a test to verify my comments, need someone to correct me if I am wrong. This note is for future reference.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐