Extract nested objects and values ​​from JSON

I am trying to extract problems from Jira and put them in a List [Issue]. I figured out how to download and parse JSON:

val json = JsonParser.parse(content)

I can also extract some numbers in the root of JSON:

val total = (json \ "total").extract[Int]
val maxResults = (json \ "maxResults").extract[Int]
println("Received " + total + " from " + maxResults + " issues")

But when I try to extract a list of all the problems

val issues = (json \ "issues")
println(issues)
issues.extract[List[Issue]]

I get an error: Exception in thread "main" net.liftweb.json.MappingException: no useful value for id I don’t know how to convert JString (13604) to int I don’t understand why it cannot convert 13604 to Int. Here is my case class:

case class Issue(id: Int,
    key: String,
    summary: String,
    issueTypeName: String,
    resolutionName: Option[String],
    resolutionDate: Option[DateTime],
    timeSpent: Option[Int],
    creatorName: String,
    reporterName: String,
    updated: DateTime,
    created: DateTime,
    priorityName: String,
    description: String,
    dueDate: Option[DateTime],
    statusName: String,
    assigneeName: String,
    projectId: Int,
    projectKey: String,
    projectName: String,
    timeEstimate: Option[Int],
    ownerName: String,
    timeOriginalEstimate: Option[Int]
                    )
  • Can someone help me with this Int problem?

  • , JSON , , , . json \ "issues", - , , JSON , . ? , - :

    (issue < - issues) { val id = (issue\ "id" ). extract [Int] Println (ID) }

issue \ "project" \ "id" , Case Class List var (, , ). :

 Error:(53, 16) value foreach is not a member of net.liftweb.json.JsonAST.JValue
        for(issue <- issues) {
                     ^

Scala Java, .


PS. id String case, :

Exception in thread "main" net.liftweb.json.MappingException: No usable value for summary Did not find value which can be converted into java.lang.String

, "summary" "". , - : 2. ?

: 3. Int id - ?

+4
2

:

val issues = (json \\ "issues").children
 for (issue <- issues) {
        val item = new Issue (
                    (issue \ "id").extract[String].toInt,
                    (issue \ "fields" \ "summary").extract[String],
                    (issue \ "fields" \ "issuetype" \ "name").extract[String],
                    (issue \ "fields" \ "resolutiondate").extractOrNone[String] match {
                        case None => None
                        case Some (null) => None
                        case Some (dt) => Some (dateTimeFormatter.withZoneUTC ().parseDateTime (dt))
                    },
        etc...) 
}

case (, ) json-, " " , .

+2

, id json ( "id": "10230" https://developer.atlassian.com/display/JIRADEV/JIRA+REST+API+Example+-+Query+issues), liftjson JString. case id .

2) case json. json,

{
  "id": "10230",
  "fields": {
    "summary": "testing"
  }
}

case

case class Issues(
   id: String,
   fields: Summary
)

case class Summary(
   summary: String
)

3) , Int extract, JSON String. .

+4

Source: https://habr.com/ru/post/1546482/


All Articles