fastjson在scala类中和java类中使用区别
在java类中
DataBeanEntitydataBeanEntity=JSONObject.parseObject(data2,DataBeanEntity.class);
DataBeanEntity dataBeanEntity2 = JSON.parseObject(data2, DataBeanEntity.class);
两种parseObject都可以使用
但是在scala类中
第一种不能使用,第二种可以使用
fastjson使用JSON.parseObject()时,为了减少输出字节会省略空值,下面附解决办法
package sparkStreaming
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.serializer.SerializerFeature
object TestFastJson {
def main(args: Array[String]): Unit = {
/**
* 解决fastjson 输出控制省略null值的问题
*/
val str = "{\"platform\": \"jingdong_1\", \"id\": \"4440141\", \"url\": \"https://item.jd.com/4440141.html\", \"title\": \"HLA\\u6d77\\u6f9c\\u4e4b\\u5bb6 \\u4f11\\u95f2\\u88e4\\u7537 HKCAD3V157A \\u4fee\\u8eab\\u6c34\\u6d17\\u7a84\\u811a \\u85cf\\u9752 170/74A(30)\", \"quantity_has\": false, \"brand_name\": \"\\u6d77\\u6f9c\\u4e4b\\u5bb6\", \"brand_id\": \"7835\", \"img_url\": \"https://img14.360buyimg.com/n1/jfs/t6718/229/1604821559/82132/cfedff08/5954968bNfb9ebc4f.jpg\", \"img_urls\": [\"jfs/t6718/229/1604821559/82132/cfedff08/5954968bNfb9ebc4f.jpg\", \"jfs/t5668/327/4722133987/88332/11277f44/5954968bNb1f02c06.jpg\", \"jfs/t5575/105/4729147849/283459/f02a92b2/5954968dN3c018476.jpg\", \"jfs/t6034/190/3529549176/275477/ac9246a4/5954968eN526560bd.jpg\", \"jfs/t6427/257/1613173464/284476/cd48b36e/5954968eN63d6e6f6.jpg\"], \"invalid_flag\": false, \"property_map\": [{\"sizeSequence\": \"2\", \"spec\": \"\", \"specName\": \"\", \"color\": \"\\u85cf\\u9752\", \"specSequence\": \"\", \"skuId\": \"4440141\", \"size\": \"170/74A(30)\", \"colorSequence\": \"1\"}], \"shop\": \"\\u6d77\\u6f9c\\u4e4b\\u5bb6\\uff08HLA\\uff09\\u4eac\\u4e1c\\u81ea\\u8425\\u65d7\\u8230\\u5e97\", \"shop_url\": \"http://mall.jd.com/index-1000073441.html\", \"root_cat_id\": \"1315\", \"cat_id\": \"1342\", \"cat3_id\": \"9736\", \"cat_name_1\": \"\\u670d\\u9970\\u5185\\u8863\", \"cat_name_2\": \"\\u7537\\u88c5\", \"cat_name_3\": \"\\u4f11\\u95f2\\u88e4\", \"thirdpart_flag\": \"false\", \"info\": {\"def\": {\"platform_price\": \"298.00\", \"vip_price\": null}}, \"crawl_time\": \"1546463376\", \"quantity\": 34, \"curr_type\": null, \"curr_source\": null, \"curr_start_time\": null, \"curr_end_time\": null, \"future_type\": null, \"future_start_time\": null, \"future_end_time\": null, \"prom_info\": [], \"prom_id\": null, \"prom_key\": null, \"prom_prom_msg\": null, \"prom_start_time\": null, \"prom_end_time\": null, \"coupon\": [], \"coupon_key\": null, \"coupon_type\": null, \"coupon_name\": null, \"coupon_limit_type\": null, \"coupon_end_time\": null, \"coupon_start_time\": null, \"coupon_url\": null, \"coupon_to_url\": null, \"coupon_gt\": null, \"coupon_minus\": null, \"coupon_batch_id\": null, \"coupon_role_id\": null, \"coupon_money_limit\": null, \"coupon_deal_type\": null, \"deposit_deal\": null, \"deposit_type\": null, \"deposit_promotion\": null, \"event_type\": null, \"platform_price\": 298.0, \"original_price\": -1, \"vip_price\": -1, \"vip_finally_price\": -1.0, \"vip_future_price\": -1.0, \"finally_price\": 298.0, \"curr_price\": 298.0, \"future_price\": -1, \"future_curr_price\": -1.0, \"mobile_price\": -1.0, \"wechat_price\": -1.0, \"prom_price\": -1, \"coupon_price\": -1, \"deposit_price\": -1.0, \"pingou_price\": -1.0, \"event_price\": -1.0}"
JSON.parseObject(str)
val text: String = JSON.toJSONString(JSON.parseObject(str),
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteMapNullValue,
SerializerFeature.PrettyFormat)
println(JSON.parseObject(str))
println(str)
println(text)
}
}```
**输出结果------------------------------------------------**
1:{"original_price":-1,"cat_name_3":"休闲裤","platform_price":298.0,"curr_price":298.0,"cat_name_1":"服饰内衣","cat_name_2":"男装","quantity_has":false,"property_map":[{"specName":"","color":"藏青","size":"170/74A(30)","sizeSequence":"2","specSequence":"","colorSequence":"1","spec":"","skuId":"4440141"}],"crawl_time":"1546463376","coupon_price":-1,"id":"4440141","future_curr_price":-1.0,"info":{"def":{"platform_price":"298.00"}},"mobile_price":-1.0,"vip_finally_price":-1.0,"brand_name":"海澜之家","brand_id":"7835","img_url":"https://img14.360buyimg.com/n1/jfs/t6718/229/1604821559/82132/cfedff08/5954968bNfb9ebc4f.jpg","vip_price":-1,"future_price":-1,"event_price":-1.0,"shop":"海澜之家(HLA)京东自营旗舰店","title":"HLA海澜之家 休闲裤男 HKCAD3V157A 修身水洗窄脚 藏青 170/74A(30)","platform":"jingdong_1","invalid_flag":false,"wechat_price":-1.0,"shop_url":"http://mall.jd.com/index-1000073441.html","pingou_price":-1.0,"cat_id":"1342","finally_price":298.0,"prom_info":[],"quantity":34,"coupon":[],"prom_price":-1,"url":"https://item.jd.com/4440141.html","root_cat_id":"1315","thirdpart_flag":"false","img_urls":["jfs/t6718/229/1604821559/82132/cfedff08/5954968bNfb9ebc4f.jpg","jfs/t5668/327/4722133987/88332/11277f44/5954968bNb1f02c06.jpg","jfs/t5575/105/4729147849/283459/f02a92b2/5954968dN3c018476.jpg","jfs/t6034/190/3529549176/275477/ac9246a4/5954968eN526560bd.jpg","jfs/t6427/257/1613173464/284476/cd48b36e/5954968eN63d6e6f6.jpg"],"cat3_id":"9736","vip_future_price":-1.0,"deposit_price":-1.0}
2:{"platform": "jingdong_1", "id": "4440141", "url": "https://item.jd.com/4440141.html", "title": "HLA\u6d77\u6f9c\u4e4b\u5bb6 \u4f11\u95f2\u88e4\u7537 HKCAD3V157A \u4fee\u8eab\u6c34\u6d17\u7a84\u811a \u85cf\u9752 170/74A(30)", "quantity_has": false, "brand_name": "\u6d77\u6f9c\u4e4b\u5bb6", "brand_id": "7835", "img_url": "https://img14.360buyimg.com/n1/jfs/t6718/229/1604821559/82132/cfedff08/5954968bNfb9ebc4f.jpg", "img_urls": ["jfs/t6718/229/1604821559/82132/cfedff08/5954968bNfb9ebc4f.jpg", "jfs/t5668/327/4722133987/88332/11277f44/5954968bNb1f02c06.jpg", "jfs/t5575/105/4729147849/283459/f02a92b2/5954968dN3c018476.jpg", "jfs/t6034/190/3529549176/275477/ac9246a4/5954968eN526560bd.jpg", "jfs/t6427/257/1613173464/284476/cd48b36e/5954968eN63d6e6f6.jpg"], "invalid_flag": false, "property_map": [{"sizeSequence": "2", "spec": "", "specName": "", "color": "\u85cf\u9752", "specSequence": "", "skuId": "4440141", "size": "170/74A(30)", "colorSequence": "1"}], "shop": "\u6d77\u6f9c\u4e4b\u5bb6\uff08HLA\uff09\u4eac\u4e1c\u81ea\u8425\u65d7\u8230\u5e97", "shop_url": "http://mall.jd.com/index-1000073441.html", "root_cat_id": "1315", "cat_id": "1342", "cat3_id": "9736", "cat_name_1": "\u670d\u9970\u5185\u8863", "cat_name_2": "\u7537\u88c5", "cat_name_3": "\u4f11\u95f2\u88e4", "thirdpart_flag": "false", "info": {"def": {"platform_price": "298.00", "vip_price": null}}, "crawl_time": "1546463376", "quantity": 34, "curr_type": null, "curr_source": null, "curr_start_time": null, "curr_end_time": null, "future_type": null, "future_start_time": null, "future_end_time": null, "prom_info": [], "prom_id": null, "prom_key": null, "prom_prom_msg": null, "prom_start_time": null, "prom_end_time": null, "coupon": [], "coupon_key": null, "coupon_type": null, "coupon_name": null, "coupon_limit_type": null, "coupon_end_time": null, "coupon_start_time": null, "coupon_url": null, "coupon_to_url": null, "coupon_gt": null, "coupon_minus": null, "coupon_batch_id": null, "coupon_role_id": null, "coupon_money_limit": null, "coupon_deal_type": null, "deposit_deal": null, "deposit_type": null, "deposit_promotion": null, "event_type": null, "platform_price": 298.0, "original_price": -1, "vip_price": -1, "vip_finally_price": -1.0, "vip_future_price": -1.0, "finally_price": 298.0, "curr_price": 298.0, "future_price": -1, "future_curr_price": -1.0, "mobile_price": -1.0, "wechat_price": -1.0, "prom_price": -1, "coupon_price": -1, "deposit_price": -1.0, "pingou_price": -1.0, "event_price": -1.0}
3:{
"original_price":-1,
"cat_name_3":"休闲裤",
"platform_price":298.0,
"prom_start_time":null,
"coupon_deal_type":null,
"curr_source":null,
"cat3_id":"9736",
"coupon_batch_id":null,
"vip_future_price":-1.0,
"deposit_price":-1.0
}