1. Jackson was expecting double-quote to start field name.
When parsing the following json text to object, parser will complain field name does not start with double-quote.
JsonParseException: Unexpected character ('d' (code 100)): was expecting double-quote to start field name
The JSON file is following:
{data:[{S:"601318",SYMBOL:"sh601318"}],date:"2014-08-29 00:00:00",num:300}
To suppress the error, enable the following configuration:
ObjectMapper om = new ObjectMapper();
om.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
2. Jackson Failed To Parse Date From String
Error message is following:
org.codehaus.jackson.map.JsonMappingException: Can not construct instance of
java.util.Date from String value '2013-08-14 00:00:00': not a valid
representation.
To fix, configure customized date format for object mapping.
ObjectMapper om = new ObjectMapper();
om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
3. Json Text containing same string with different cases.
For example, a POJO parsing the following will always have error message like property conflict on SYMBOL and symbol if you have both “SYMBOL” and “symbol” as property of the POJO.
{
S:"601318",
SYMBOL:"sh601318", <---
SNAME:"中国平安",
WEIGHING:"3.77",
INAME:"金融地产",
HYSYMBOL:"sh000914",
zgb:7991402541.3895,
symbol:"sh601318", <---
volume:"49375218",
}
The solution I could found is to make properties public, and abandon the getter/setter methods.
Example that works for parsing sina stock price
class Sina300Page{
private List<Sina300ETFStockInfo> data;
private Date date;
private int num;
public List<Sina300ETFStockInfo> getData() {
return data;
}
public void setData(List<Sina300ETFStockInfo> data) {
this.data = data;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
class Sina300ETFStockInfo{
public String S; //:"600150",
public String SYMBOL;//:"sh600150",
public String symbol;//:"sh600150",
public String SNAME;//SNAME:"中国船舶",
public String WEIGHING; //WEIGHING:"0.31",
public String INAME; //INAME:"工业",
public String HYSYMBOL; //HYSYMBOL:"sh000910",
public double ltgb; //ltgb:1426911053.5423,
public double zgb; //zgb:1426911053.5423,
public String name; //name:"中国船舶",
public String prevtrade; //prevtrade:"23.340",
public String pricechange; //pricechange:"1.190",
public String changepercent; //changepercent:"5.373",
public String buy; //buy:"23.340",
public String sell; //sell:"23.360",
public String settlement; //settlement:"22.150",
public String open; //open:"22.420",
public String high; //high:"23.700",
public String low; //low:"22.400",
public String volume; //volume:"27162579",
public String amount; //amount:"630928382",
public double gxzs; //gxzs:0.537,
public double ltsz; //ltsz:33304103989.677}
}
public class Application implements CommandLineRunner{
private static Logger logger = LogManager.getLogger(Application.class);
@Autowired
private Environment env;
@Autowired
private EquityHistoryPersister equityHistoryPersister;
@Autowired
private EquityScraper equityScraper;
public static void main(String[] args) {
System.setProperty("app.home", "H:/java/ichihedge-ws/commons-dao/src/main/java/com/ichihedge/emc/app");
SpringApplication.run(Application.class, args);
}
public void TestSinaJson(){
ObjectMapper om = new ObjectMapper();
//om.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, false);
om.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
try{
Sina300Page page = om.readValue(new File("H:\\java\\ichihedge-ws\\Documentation\\json-data.txt"), Sina300Page.class);
//for(int i=0; i<page.getData().size();i++){
Sina300ETFStockInfo index = page.getData().get(0);
logger.info(index.amount+" amount");
logger.info(index.buy+" buy");
logger.info(index.changepercent+" changePercent");
logger.info(index.gxzs+" gxzs");
logger.info(index.high+" high");
logger.info(index.HYSYMBOL+" hysymbol");
logger.info(index.INAME+" iname");
logger.info(index.low+" low");
logger.info(index.ltgb+" ltgb");
logger.info(index.ltsz+" ltgz");
logger.info(index.name+" name");
logger.info(index.open+" open");
logger.info(index.prevtrade+" prevTrader");
logger.info(index.pricechange+" priceChange");
logger.info(index.S+" S");
logger.info(index.sell+" sell");
logger.info(index.settlement+" settlement");
logger.info(index.SNAME+" sname");
logger.info(index.SYMBOL+" SYMBOL");
logger.info(index.symbol+" symbol");
logger.info(index.volume+" volume");
logger.info(index.WEIGHING+" weighing");
logger.info(index.zgb+" zgb");
//}
}catch(Exception e){
e.printStackTrace();
}
}
}
//json-data file has following content
{data:[{S:"601318",SYMBOL:"sh601318",SNAME:"中国平安",WEIGHING:"3.77",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:4831933910.0356,zgb:7991402541.3895,symbol:"sh601318",name:"中国平安",prevtrade:"34.400",pricechange:"-0.220",changepercent:"-0.636",buy:"34.400",sell:"34.410",settlement:"34.600",open:"34.620",high:"34.750",low:"34.250",volume:"49375218",amount:"1701605543",gxzs:-0.773,ltsz:166218526505.22},{S:"600036",SYMBOL:"sh600036",SNAME:"招商银行",WEIGHING:"3.23",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:20806270598.877,zgb:25436635104.734,symbol:"sh600036",name:"招商银行",prevtrade:"18.310",pricechange:"0.030",changepercent:"0.164",buy:"18.310",sell:"18.330",settlement:"18.300",open:"18.330",high:"18.490",low:"18.260",volume:"12929420",amount:"237561461",gxzs:0.171,ltsz:380962814665.43},{S:"600016",SYMBOL:"sh600016",SNAME:"民生银行",WEIGHING:"3.14",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:27368361385.803,zgb:34369039949.365,symbol:"sh600016",name:"民生银行",prevtrade:"9.310",pricechange:"-0.020",changepercent:"-0.214",buy:"9.310",sell:"9.320",settlement:"9.330",open:"9.320",high:"9.340",low:"9.280",volume:"34307040",amount:"319143727",gxzs:-0.217,ltsz:254799444501.83},{S:"601166",SYMBOL:"sh601166",SNAME:"兴业银行",WEIGHING:"2.19",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:16434413814.402,zgb:19352373550.228,symbol:"sh601166",name:"兴业银行",prevtrade:"16.040",pricechange:"0.010",changepercent:"0.062",buy:"16.030",sell:"16.040",settlement:"16.030",open:"16.040",high:"16.060",low:"16.000",volume:"27808012",amount:"445754946",gxzs:0.044,ltsz:263607997583},{S:"600000",SYMBOL:"sh600000",SNAME:"浦发银行",WEIGHING:"1.96",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:15098151649.577,zgb:18872689561.971,symbol:"sh600000",name:"浦发银行",prevtrade:"16.580",pricechange:"0.020",changepercent:"0.121",buy:"16.560",sell:"16.580",settlement:"16.550",open:"16.570",high:"16.580",low:"16.500",volume:"12690366",amount:"209785537",gxzs:0.076,ltsz:250327354349.98},{S:"600030",SYMBOL:"sh600030",SNAME:"中信证券",WEIGHING:"1.88",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:10008780800.079,zgb:11234806113.608,symbol:"sh600030",name:"中信证券",prevtrade:"16.510",pricechange:"0.110",changepercent:"0.670",buy:"16.520",sell:"16.530",settlement:"16.410",open:"16.410",high:"16.580",low:"16.350",volume:"78154011",amount:"1285859995",gxzs:0.406,ltsz:165244971009.31},{S:"000002",SYMBOL:"sz000002",SNAME:"万科A",WEIGHING:"1.64",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:9779140507.8492,zgb:11137144936.284,symbol:"sz000002",name:"万 科A",prevtrade:"26.230",pricechange:"0.260",changepercent:"1.001",buy:"26.220",sell:"26.230",settlement:"25.970",open:"25.750",high:"27.410",low:"25.560",volume:"150728616",amount:"4017712913",gxzs:0.529,ltsz:256506855520.88},{S:"600837",SYMBOL:"sh600837",SNAME:"海通证券",WEIGHING:"1.47",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:8226162338.2609,zgb:9743474325.8385,symbol:"sh600837",name:"海通证券",prevtrade:"16.100",pricechange:"0.040",changepercent:"0.249",buy:"16.090",sell:"16.100",settlement:"16.060",open:"16.070",high:"16.140",low:"16.010",volume:"13552239",amount:"217952728",gxzs:0.118,ltsz:132441213646},{S:"600519",SYMBOL:"sh600519",SNAME:"贵州茅台",WEIGHING:"1.34",INAME:"主要消费",HYSYMBOL:"sh000912",ltgb:1158174231.1242,zgb:1158174231.1242,symbol:"sh600519",name:"贵州茅台",prevtrade:"308.860",pricechange:"5.960",changepercent:"1.967",buy:"308.800",sell:"308.900",settlement:"302.960",open:"304.000",high:"309.000",low:"303.000",volume:"4173675",amount:"1279755341",gxzs:0.85,ltsz:357713693025.02},{S:"000651",SYMBOL:"sz000651",SNAME:"格力电器",WEIGHING:"1.27",INAME:"可选消费",HYSYMBOL:"sh000911",ltgb:3039932234.7249,zgb:3039932234.7249,symbol:"sz000651",name:"格力电器",prevtrade:"22.110",pricechange:"0.000",changepercent:"0.000",buy:"22.110",sell:"22.120",settlement:"22.110",open:"22.080",high:"22.220",low:"21.910",volume:"44485825",amount:"981783491",gxzs:0,ltsz:67212901709.768},{S:"601328",SYMBOL:"sh601328",SNAME:"交通银行",WEIGHING:"1.22",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:33104090222.159,zgb:75159619478.877,symbol:"sh601328",name:"交通银行",prevtrade:"5.570",pricechange:"-0.030",changepercent:"-0.536",buy:"5.570",sell:"5.580",settlement:"5.600",open:"5.590",high:"5.590",low:"5.560",volume:"45368217",amount:"252796185",gxzs:-0.211,ltsz:184389782537.43},{S:"600887",SYMBOL:"sh600887",SNAME:"伊利股份",WEIGHING:"1.21",INAME:"主要消费",HYSYMBOL:"sh000912",ltgb:3047989205.4025,zgb:3105291668.6944,symbol:"sh600887",name:"伊利股份",prevtrade:"0.000",pricechange:"0.000",changepercent:"0.000",buy:"0.000",sell:"0.000",settlement:"16.110",open:"0.000",high:"0.000",low:"0.000",volume:"0",amount:"0",gxzs:0,ltsz:0},{S:"601288",SYMBOL:"sh601288",SNAME:"农业银行",WEIGHING:"1.17",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:286521732758.8,zgb:327489503863.07,symbol:"sh601288",name:"农业银行",prevtrade:"3.140",pricechange:"-0.010",changepercent:"-0.318",buy:"3.140",sell:"3.150",settlement:"3.150",open:"3.150",high:"3.150",low:"3.140",volume:"65399176",amount:"205828949",gxzs:-0.12,ltsz:899678240862.63},{S:"601601",SYMBOL:"sh601601",SNAME:"中国太保",WEIGHING:"1.12",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:6336097852.2787,zgb:9133204819.2771,symbol:"sh601601",name:"中国太保",prevtrade:"29.850",pricechange:"0.390",changepercent:"1.322",buy:"29.850",sell:"29.920",settlement:"29.510",open:"29.590",high:"30.070",low:"29.310",volume:"7773808",amount:"231144403",gxzs:0.478,ltsz:189132520890.52},{S:"601398",SYMBOL:"sh601398",SNAME:"工商银行",WEIGHING:"1.1",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:266217129750.6,zgb:353515790838.68,symbol:"sh601398",name:"工商银行",prevtrade:"4.460",pricechange:"-0.010",changepercent:"-0.224",buy:"4.450",sell:"4.460",settlement:"4.460",open:"4.460",high:"4.460",low:"4.440",volume:"55342680",amount:"246098904",gxzs:-0.079,ltsz:1187328398687.7},{S:"000001",SYMBOL:"sz000001",SNAME:"平安银行",WEIGHING:"1.08",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:6757040333.7192,zgb:11537455326.773,symbol:"sz000001",name:"平安银行",prevtrade:"9.150",pricechange:"0.030",changepercent:"0.329",buy:"9.140",sell:"9.150",settlement:"9.120",open:"9.130",high:"9.150",low:"9.110",volume:"36337078",amount:"331886515",gxzs:0.115,ltsz:61826919053.531},{S:"600104",SYMBOL:"sh600104",SNAME:"上汽集团",WEIGHING:"1.04",INAME:"可选消费",HYSYMBOL:"sh000911",ltgb:9448925074.1926,zgb:11271910810.928,symbol:"sh600104",name:"上汽集团",prevtrade:"22.390",pricechange:"-0.080",changepercent:"-0.356",buy:"22.390",sell:"22.400",settlement:"22.470",open:"22.540",high:"22.540",low:"22.220",volume:"15734229",amount:"351921558",gxzs:-0.119,ltsz:211561432411.17},{S:"601818",SYMBOL:"sh601818",SNAME:"光大银行",WEIGHING:"0.98",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:40570099185.115,zgb:47569917423.664,symbol:"sh601818",name:"光大银行",prevtrade:"3.810",pricechange:"-0.020",changepercent:"-0.524",buy:"3.800",sell:"3.810",settlement:"3.820",open:"3.830",high:"3.830",low:"3.790",volume:"66367634",amount:"252651792",gxzs:-0.166,ltsz:154572077895.29},{S:"601088",SYMBOL:"sh601088",SNAME:"中国神华",WEIGHING:"0.92",INAME:"能源",HYSYMBOL:"sh000908",ltgb:16702034400.215,zgb:20144100447.354,symbol:"sh601088",name:"中国神华",prevtrade:"15.530",pricechange:"0.130",changepercent:"0.844",buy:"15.530",sell:"15.550",settlement:"15.410",open:"15.480",high:"15.680",low:"15.440",volume:"16777745",amount:"260859548",gxzs:0.25,ltsz:259382594235.35},{S:"601668",SYMBOL:"sh601668",SNAME:"中国建筑",WEIGHING:"0.87",INAME:"工业",HYSYMBOL:"sh000910",ltgb:30141193183.28,zgb:30289389067.524,symbol:"sh601668",name:"中国建筑",prevtrade:"6.180",pricechange:"0.010",changepercent:"0.162",buy:"6.170",sell:"6.180",settlement:"6.160",open:"6.170",high:"6.220",low:"6.140",volume:"79311038",amount:"489585637",gxzs:0.045,ltsz:186272573872.67},{S:"601169",SYMBOL:"sh601169",SNAME:"北京银行",WEIGHING:"0.86",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:9155036374.5084,zgb:10780808259.96,symbol:"sh601169",name:"北京银行",prevtrade:"9.100",pricechange:"-0.020",changepercent:"-0.219",buy:"9.100",sell:"9.110",settlement:"9.120",open:"9.130",high:"9.150",low:"9.080",volume:"29878491",amount:"272018944",gxzs:-0.061,ltsz:83310831008.026},{S:"601006",SYMBOL:"sh601006",SNAME:"大秦铁路",WEIGHING:"0.82",INAME:"工业",HYSYMBOL:"sh000910",ltgb:15091118934.623,zgb:15091118934.623,symbol:"sh601006",name:"大秦铁路",prevtrade:"6.500",pricechange:"0.000",changepercent:"0.000",buy:"6.490",sell:"6.500",settlement:"6.490",open:"6.530",high:"6.540",low:"6.460",volume:"44133843",amount:"286924439",gxzs:0,ltsz:98092273075.048},{S:"600383",SYMBOL:"sh600383",SNAME:"金地集团",WEIGHING:"0.75",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:4471508572,zgb:4471508572,symbol:"sh600383",name:"金地集团",prevtrade:"12.000",pricechange:"0.000",changepercent:"0.000",buy:"11.990",sell:"12.000",settlement:"12.000",open:"11.960",high:"12.150",low:"11.870",volume:"15030775",amount:"180630083",gxzs:0,ltsz:53658102864},{S:"601939",SYMBOL:"sh601939",SNAME:"建设银行",WEIGHING:"0.71",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:9641625894.03,zgb:251261032373.43,symbol:"sh601939",name:"建设银行",prevtrade:"5.200",pricechange:"0.000",changepercent:"0.000",buy:"5.200",sell:"5.210",settlement:"5.210",open:"5.220",high:"5.220",low:"5.190",volume:"48047380",amount:"250199358",gxzs:0,ltsz:50136454648.956},{S:"600015",SYMBOL:"sh600015",SNAME:"华夏银行",WEIGHING:"0.7",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:6589301182.7386,zgb:9044113829.0205,symbol:"sh600015",name:"华夏银行",prevtrade:"10.200",pricechange:"0.050",changepercent:"0.493",buy:"10.190",sell:"10.200",settlement:"10.150",open:"10.170",high:"10.210",low:"10.120",volume:"40284336",amount:"409836661",gxzs:0.111,ltsz:67210872063.934},{S:"000333",SYMBOL:"sz000333",SNAME:"美的集团",WEIGHING:"0.69",INAME:"可选消费",HYSYMBOL:"sh000911",ltgb:1731537572.6606,zgb:4254485613.945,symbol:"sz000333",name:"美的集团",prevtrade:"27.000",pricechange:"0.050",changepercent:"0.186",buy:"26.990",sell:"27.000",settlement:"26.950",open:"26.900",high:"27.140",low:"26.680",volume:"22004789",amount:"592389006",gxzs:0.041,ltsz:46751514461.836},{S:"600900",SYMBOL:"sh600900",SNAME:"长江电力",WEIGHING:"0.68",INAME:"公用事业",HYSYMBOL:"sh000917",ltgb:10126536126.36,zgb:17144351464.435,symbol:"sh600900",name:"长江电力",prevtrade:"13.480",pricechange:"0.130",changepercent:"0.974",buy:"13.470",sell:"13.480",settlement:"13.350",open:"13.310",high:"13.540",low:"13.290",volume:"17214016",amount:"231111950",gxzs:0.214,ltsz:136505706983.33},{S:"600048",SYMBOL:"sh600048",SNAME:"保利地产",WEIGHING:"0.67",INAME:"金融地产",HYSYMBOL:"sh000914",ltgb:10763943669.91,zgb:10763943669.91,symbol:"sh600048",name:"保利地产",prevtrade:"9.250",pricechange:"-0.010",changepercent:"-0.108",buy:"9.250",sell:"9.260",settlement:"9.260",open:"9.190",high:"9.380",low:"9.180",volume:"53655557",amount:"497446319",gxzs:-0.023,ltsz:99566478946.664},{S:"000858",SYMBOL:"sz000858",SNAME:"五粮液",WEIGHING:"0.66",INAME:"主要消费",HYSYMBOL:"sh000912",ltgb:3870356922.4605,zgb:3870356922.4605,symbol:"sz000858",name:"五 粮 液",prevtrade:"33.470",pricechange:"-0.200",changepercent:"-0.594",buy:"33.470",sell:"33.480",settlement:"33.670",open:"33.770",high:"33.870",low:"33.210",volume:"22611729",amount:"757048019",gxzs:-0.126,ltsz:129540846194.75},{S:"601989",SYMBOL:"sh601989",SNAME:"中国重工",WEIGHING:"0.66",INAME:"工业",HYSYMBOL:"sh000910",ltgb:16117342215.454,zgb:18215161712.813,symbol:"sh601989",name:"中国重工",prevtrade:"6.650",pricechange:"0.320",changepercent:"5.047",buy:"6.650",sell:"6.660",settlement:"6.340",open:"6.410",high:"6.730",low:"6.400",volume:"163694073",amount:"1083836741",gxzs:1.074,ltsz:107180325732.77}],date:"2014-08-29 00:00:00",num:300}
Reference:
1. Parsing Error On Json Attribute
http://www.mkyong.com/java/jackson-was-expecting-double-quote-to-start-field-name/
2. Example Code To Get Json Working
http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/