Home > Web Front-end > JS Tutorial > How Scala parses Json strings

How Scala parses Json strings

小云云
Release: 2018-01-30 09:35:30
Original
2168 people have browsed it

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

Detailed explanation of examples of Scala parsing Json strings

1. Add corresponding dependencies

Use of Json parsing tools json-smart has been compared with 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>
Copy after login

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)
  }

 }
}
Copy after login


The above is the detailed content of How Scala parses Json strings. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template