实体类
`package web.zhangyong168.cn.action.entity
import java.util.Date
import com.alibaba.fastjson.JSONObject
object District {
val pkColumn: Set[String] = Set( "id", "data_date" )
}
case class District(
id: Int,
district_name: String,
city_id: Int,
isdel: Int,
data_date: Date,
city:JSONObject //List[String]
) {
}
`
业务模型拆解类
`package web.zhangyong168.cn.action.entity
import com.alibaba.fastjson.{JSON, JSONArray, JSONObject}
/**
*
- @description 业务模型拆解技巧
- @author 张勇
- @version 0.0.1
- @date 2021年03月13日下午13:50:20
*/
object JSONEntity {
val jsonTest: String =
s"""
|{
|serialid:1,
|provincename:"云南",
|intime:"2016-07-06 10:09:57",
|updatetime:"2016-07-06 10:09:57"
|}
|""".stripMargin
val jsonTest_2: String =
s"""
|{
| "id":1,
| "name":"daniel",
| "age":20,
| "city_name":"上海市",
| "province_id":"31",
| "list":{
| "serialId":1,
| "provinceName":"云南",
| "intime":"2016-07-06 10:09:57",
| "updatetime":"2016-07-06 10:09:57"
| }
|}
|""".stripMargin
val jsonTest_3: String =
s"""
|
|[
|{"district_name":"朔城区","city":{"city_name":"朔州市","province_id":10,"id":100},"id":1379,"city_id":100},
|{"district_name":"平鲁区","city":{"city_name":"朔州市","province_id":10,"id":100},"id":1380,"city_id":100},
|{"district_name":"山阴县","city":{"city_name":"朔州市","province_id":10,"id":100},"id":1381,"city_id":100},
|{"district_name":"应县","city":{"city_name":"朔州市","province_id":10,"id":100},"id":1382,"city_id":100},
|{"district_name":"右玉县","city":{"city_name":"朔州市","province_id":10,"id":100},"id":1383,"city_id":100},
|{"district_name":"怀仁县","city":{"city_name":"朔州市","province_id":10,"id":100},"id":1384,"city_id":100},
|{"district_name":"鹿城区","city":{"city_name":"温州市","province_id":18,"id":222},"id":1780,"city_id":222},
|{"district_name":"龙湾区","city":{"city_name":"温州市","province_id":18,"id":222},"id":1781,"city_id":222},
|{"district_name":"瓯海区","city":{"city_name":"温州市","province_id":18,"id":222},"id":1782,"city_id":222},
|{"district_name":"洞头县","city":{"city_name":"温州市","province_id":18,"id":222},"id":1783,"city_id":222},
|{"district_name":"永嘉县","city":{"city_name":"温州市","province_id":18,"id":222},"id":1784,"city_id":222},
|{"district_name":"平阳县","city":{"city_name":"温州市","province_id":18,"id":222},"id":1785,"city_id":222},
|{"district_name":"苍南县","city":{"city_name":"温州市","province_id":18,"id":222},"id":1786,"city_id":222},
|{"district_name":"文成县","city":{"city_name":"温州市","province_id":18,"id":222},"id":1787,"city_id":222},
|{"district_name":"泰顺县","city":{"city_name":"温州市","province_id":18,"id":222},"id":1788,"city_id":222},
|{"district_name":"瑞安市","city":{"city_name":"温州市","province_id":18,"id":222},"id":1789,"city_id":222},
|{"district_name":"乐清市","city":{"city_name":"温州市","province_id":18,"id":222},"id":1790,"city_id":222}
|]
|
|""".stripMargin
val json = JSON.parseObject( jsonTest_2 )
println( "json:" + json.toString )
val list = json.get( "list" )
val resJson = JSON.parseObject( list.toString, classOf[Province] )
val array: JSONArray = JSON.parseArray( jsonTest_3 )
val keys: Set[String] = Set( "a", "city_name", "dd" )
def isNull(keys: Set[String], json: JSONObject): Boolean = {
val flag = keys.foreach( key => {
if (json.get( key ) == null) {
false
}
} )
println( "flag:" + flag )
true
}
//1.2.62 或者1.2.70
def main(args: Array[String]): Unit = {
println( "flag:" + isNull( keys, json ) )
println( "array:" + array )
for (i <- 0 until array.size()) {
val ad_json = JSON.parseObject( array.get( i ).toString, classOf[District] )
println( "ad:" + ad_json )
}
// println("provinceName:"+resJson.provinceName)
}
}
`