Home >Web Front-end >JS Tutorial >Detailed explanation of Scala parsing Json string examples

Detailed explanation of Scala parsing Json string examples

小云云
小云云Original
2018-01-25 10:27:433343browse

This article mainly introduces relevant information about the detailed explanation of examples of Scala parsing Json strings. I hope this article can help everyone learn and understand this part of the content. Friends who need it can refer to it. I hope it can help everyone.

Detailed explanation of examples of Scala parsing Json strings

1. Add corresponding dependencies

json used by Json parsing tool -smart, I have compared Java's fastjson and gson. Scala’s json4s, lift-json. Among them, json-smart has the fastest parsing speed.


  <dependency>
      <groupId>net.minidev</groupId>
      <artifactId>json-smart</artifactId>
      <version>2.3</version>
    </dependency>

2. Scala code


package Test

import java.util

import net.minidev.json.JSONObject
import net.minidev.json.parser.JSONParser

import scala.collection.JavaConversions._
import scala.collection.mutable
import scala.util.parsing.json.JSON

/**
 * Created by zhanghuayan on 2017/3/30.
 */
object Test {
 def main(args: Array[String]): Unit = {

  val str2 = "{\"name\":\"jeemy\",\"age\":25,\"phone\":\"18810919225\"}"
  val jsonParser = new JSONParser()

  val jsonObj: JSONObject = jsonParser.parse(str2).asInstanceOf[JSONObject]
  val name = jsonObj.get("name").toString
  println(name)

  val jsonKey = jsonObj.keySet()
  val iter = jsonKey.iterator

  while (iter.hasNext) {
   val instance = iter.next()
   val value = jsonObj.get(instance).toString
   println("key: " + instance + " value:" + value)
  }

 }
}

Related recommendations:

Detailed explanation of examples of mutual conversion between JSON strings and JSON objects

js Analysis of the method of converting json strings into json objects

jQuery TreeView tree control data supports json strings and list collections


The above is the detailed content of Detailed explanation of Scala parsing Json string examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn