Json:scala 解析json字符串,变成对象

第一种

package com.donews
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule

object Test {

  def main(args: Array[String]): Unit = {

    val mapper = new ObjectMapper()

    mapper.registerModule(DefaultScalaModule)

    val log = "{\"status\":200,\"http_user_agent\":\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36\",\"request_method\":\"get\",\"timestamp\":\"2016-11-13T06:59:26.315Z\",\"http_url\":\"http://www.donews.com/it/201611/2942496.shtm\",\"http_x_forwarded_for\":\"221.0.90.218\",\"event\":\"load\",\"is_new\":1,\"http_referer\":\"https://www.baidu.com/link?url=btPCeZwsYyyONB-oYL7szsox4qjvynFPzoeZlocefakSUxZxj0gEOXoG9dIivWg7BqsooA1tMfZpS-FYdl5Gva&wd=&eqid=c3101a590000f2980000000258280f0d\",\"cookie\":\"441de3213207ac61f58be158e80cc194\",\"page_id\":\"23788cd5e93c8c870472bd1bd40f6c40\",\"short_cookie\":\"2cb494930448e4abf37577dd28c5947f\",\"appkey\":\"app_website\"}"

    val obj = mapper.readValue(log, classOf[SdkBean])

    println(obj.toString)

  }

}
package com.donews

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

/**
  * Created by yuhui on 2016/11/22.
  */

@JsonIgnoreProperties(ignoreUnknown = true)
case class SdkBean(
             appkey: String,
             timestamp: String,
             cookie: String,
             short_cookie: String,
             request_method: String,

             status: java.lang.Integer,
             http_referer: String,
             http_user_agent: String,
             http_x_forwarded_for: String,
             http_url: String,

             to_target: String,
             duration: java.lang.Integer,
             event: String,
             is_new: java.lang.Integer,
             page_id: String,
             day: String
             )

结果展示:

SdkBean(app_website,2016-11-13T06:59:26.315Z,441de3213207ac61f58be158e80cc194,2cb494930448e4abf37577dd28c5947f,get,200,https://www.baidu.com/link?url=btPCeZwsYyyONB-oYL7szsox4qjvynFPzoeZlocefakSUxZxj0gEOXoG9dIivWg7BqsooA1tMfZpS-FYdl5Gva&wd=&eqid=c3101a590000f2980000000258280f0d,Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36,221.0.90.218,http://www.donews.com/it/201611/2942496.shtm,null,null,load,1,23788cd5e93c8c870472bd1bd40f6c40,null)

第二种

import com.google.gson.Gson

object Test extends App {

  val gson = new Gson()

  case class User1(id: Long,organization_id: Long,username: String ,role_names: String ,role_ids: String,locked: Boolean)

  println(gson.toJson(User1(1,1,"admin","admin, 图计算管理层","1,2,3,4",false)))

}

结果:

{“id”:1,“organization_id”:1,“username”:“admin”,“role_names”:“admin, 图计算管理层”,“role_ids”:“1,2,3,4”,“locked”:false}

###第三种


import com.alibaba.fastjson.JSON

  case class User(id: Long,organization_id: Long,username: String ,role_ids: String,locked: Boolean)

  val jsonstr = "{\"id\":1,\"organization_id\":1,\"username\":\"admin\",\"role_ids\":\"1,2,3,4\",\"locked\":false}"

  println(JSON.parseObject(jsonstr, classOf[User]))

结果:User(1,1,admin,1,2,3,4,false)

###第四种

Json字符串变成list对象,进行过滤之后,再次转为Json字符串


import com.alibaba.fastjson.JSON

object JsonTest {

  case class JsonClass(key: String, type2: String, name: String, grid: String, condition: String) {
    override def toString: String = {
      if (condition != null) {
        "{\"key\" : \"" + key + "\" , \"type\" : \"" + type2 + "\" , \"name\" : \"" + name + "\" , \"grid\" : " + grid + " , \"condition\" : " + condition + "}"
      } else {
        "{\"key\" : \"" + key + "\" , \"type\" : \"" + type2 + "\" , \"name\" : \"" + name + "\" , \"grid\" : " + grid + "}"
      }
    }
  }

  def main(args: Array[String]): Unit = {

    val json = JSON.parseArray(jsonStr, classOf[JsonClass]).toArray

    val nameSet = Set("企业征信图谱监控") 

    val j = json.map(row => row.asInstanceOf[JsonClass]).filterNot(r => nameSet.contains(r.name)).toList

    println(j.map(row => row.toString).mkString("[", ",", "]"))
  }

  val jsonStr =
    """
      |[{
      |	"key": "36069d72-f457-f5f8-62c6-0915ed3ba82f",
      |	"type2": "ProjectCreator",
      |	"name": "项目创建/监控",
      |	"grid": {
      |		"x": 0,
      |		"y": 0,
      |		"w": 6,
      |		"h": 2,
      |		"static": true
      |	}
      |}, {
      |	"key": "bd4b17c7-6710-014b-bc2d-72a3c1e0f49e",
      |	"type2": "ProjectFlow",
      |	"name": "企业征信图谱监控",
      |	"grid": {
      |		"x": 6,
      |		"y": 2,
      |		"w": 6,
      |		"h": 2
      |	},
      |	"condition": {
      |		"projectId": 1
      |	}
      |}, {
      |	"key": "1b787098-3d2c-4589-9c99-1d16a3cf20af",
      |	"type2": "ClusterResource",
      |	"grid": {
      |		"x": 0,
      |		"y": 6,
      |		"w": 12,
      |		"h": 3
      |	}
      |}, {
      |	"key": "91491e4c-caec-a29f-6fd6-d5deef0fb50b",
      |	"type2": "TaskList",
      |	"name": "待办事项",
      |	"grid": {
      |		"x": 0,
      |		"y": 2,
      |		"w": 6,
      |		"h": 2
      |	}
      |}, {
      |	"key": "8dd04eeb-476f-8c87-f6df-99e267e06d02",
      |	"type2": "ProjectFlow",
      |	"name": "失联修复监控",
      |	"grid": {
      |		"x": 6,
      |		"y": 0,
      |		"w": 6,
      |		"h": 2
      |	},
      |	"condition": {
      |		"projectId": 2
      |	}
      |}, {
      |	"key": "cca2bdd4-36f3-de8b-041e-1cc9920c951c",
      |	"type2": "ProjectFlow",
      |	"name": "关系网络监控",
      |	"grid": {
      |		"x": 0,
      |		"y": 4,
      |		"w": 6,
      |		"h": 2
      |	},
      |	"condition": {
      |		"projectId": 3
      |	}
      |}, {
      |	"key": "f3a449d2-5fbe-2a61-ed34-ce87c5c52266",
      |	"type2": "ProjectFlow",
      |	"name": "案件调查监控",
      |	"grid": {
      |		"x": 6,
      |		"y": 4,
      |		"w": 6,
      |		"h": 2
      |	},
      |	"condition": {
      |		"projectId": 4
      |	}
      |}, {
      |	"key": "86276e05-1bec-26bc-981c-f6bc6e49d4de",
      |	"type2": "ProjectFlow",
      |	"name": "userpin统计自然人监控",
      |	"grid": {
      |		"x": 0,
      |		"y": 9,
      |		"w": 6,
      |		"h": 2
      |	},
      |	"condition": {
      |		"projectId": 5
      |	}
      |}]
      |
    """.stripMargin

}

结果:[{“key” : “36069d72-f457-f5f8-62c6-0915ed3ba82f” , “type” : “ProjectCreator” , “name” : “项目创建/监控” , “grid” : {“static”:true,“w”:6,“x”:0,“h”:2,“y”:0}},{“key” : “1b787098-3d2c-4589-9c99-1d16a3cf20af” , “type” : “ClusterResource” , “name” : “null” , “grid” : {“w”:12,“x”:0,“h”:3,“y”:6}},{“key” : “91491e4c-caec-a29f-6fd6-d5deef0fb50b” , “type” : “TaskList” , “name” : “待办事项” , “grid” : {“w”:6,“x”:0,“h”:2,“y”:2}},{“key” : “8dd04eeb-476f-8c87-f6df-99e267e06d02” , “type” : “ProjectFlow” , “name” : “失联修复监控” , “grid” : {“w”:6,“x”:6,“h”:2,“y”:0} , “condition” : {“projectId”:2}},{“key” : “cca2bdd4-36f3-de8b-041e-1cc9920c951c” , “type” : “ProjectFlow” , “name” : “关系网络监控” , “grid” : {“w”:6,“x”:0,“h”:2,“y”:4} , “condition” : {“projectId”:3}},{“key” : “f3a449d2-5fbe-2a61-ed34-ce87c5c52266” , “type” : “ProjectFlow” , “name” : “案件调查监控” , “grid” : {“w”:6,“x”:6,“h”:2,“y”:4} , “condition” : {“projectId”:4}},{“key” : “86276e05-1bec-26bc-981c-f6bc6e49d4de” , “type” : “ProjectFlow” , “name” : “userpin统计自然人监控” , “grid” : {“w”:6,“x”:0,“h”:2,“y”:9} , “condition” : {“projectId”:5}}]

北京小辉微信公众号

Json:scala 解析json字符串,变成对象_第1张图片

大数据资料分享请关注

你可能感兴趣的:(【工,具】工具类)