1. 程式人生 > >scala fastjson解析json並封裝成scala類物件

scala fastjson解析json並封裝成scala類物件

package com.Trait

import com.alibaba.fastjson.JSON
import com.getsET.Get
import org.apache.log4j.LogManager
import org.apache.logging.log4j.scala.{Logger, Logging}
import org.apache.logging.log4j.Level
import scala.io.Source
//Logging是用log4j2進行的輸出,配置檔案在下面 
object Main extends Logging {
//下面是scala獨有的樣本類,在這裡的作用是類封裝json字串
  case class model(stcode: Int, endurl: String, ip: String, time: Long, protrp: Int, url: String, content: String, md5: String)

  def main(args: Array[String]): Unit = {
 
    val source = Source.fromFile("C:\\Users\\Administrator\\Desktop\\11-29.txt", "UTF-8")
    for (line <- source.getLines()) {
      val obj: model = JSON.parseObject(line, classOf[model])
      if (!obj.eq(null)) {
        println(obj.md5)
        log.info(obj.md5)
//        logger.info(obj.md5)}
//      println(line)
      }
    }
   

  }
}

 

 

 

 

 

#############萬惡的分割線##########萬惡的分割線##############

 

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders><!--這裡面可以寫多個appender的定義,比如說consoel  File等-->

        <RollingRandomAccessFile name="myINFO"
                                 fileName="C:\Users\Administrator\Desktop\pg\pg-%d{yyyy-MM-dd HH-mm-ss.SSS}.tmp"
                                 filePattern="C:\Users\Administrator\Desktop\pg\pg-%d{yyyy-MM-dd HH-mm-ss.SSS}.log">
            <PatternLayout
                    charset="UTF-8"  pattern="%msg%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
            <Filters>
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL" />
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
            </Filters>
            <DefaultRolloverStrategy max="100"/>
        </RollingRandomAccessFile>
    </Appenders>
    <!-- <logger name="MyThread" level="info" additivity="false">-->
    <Loggers>
        <Root level="info">
            <AppenderRef ref="myINFO"/>
        </Root>

        <logger name="Main" level="info" additivity="false">
            <Appenderf ref="myINFO"/>
        </logger>

    </Loggers>
</Configuration>