2014-06-29 81 views
4

我試圖從Jira中提取問題並將它們放入List [Issue]中。 我想通了如何下載和解析JSON:從JSON中提取嵌套實體和值

val json = JsonParser.parse(content) 

我還可以在JSON的根中提取一些數字:

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

但是,當我試圖提取所有的問題清單

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

我收到一個錯誤: 異常線程 「main」 net.liftweb.json.MappingException:沒有可用於ID值不知道如何將JString(13604)轉換爲int 我不明白爲什麼不能將13604轉換爲Int。 這裏是我的情況下類:

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] 
        ) 
  1. 誰能幫我解決這個問題的詮釋?

  2. 此外,JSON具有嵌套元素的一些屬性,如項目具有嵌套的ID,鍵和名稱。在我提取json \ "issues"的問題之前,我看到了另一個錯誤 - 我相信這是因爲JSON提取器不知道他需要去嵌套元素。 我怎樣才能讓他知道呢? 我想我可以做這樣的事情:

    的(問題< - 問題){ VAL ID =(問題\ 「ID」)提取物[INT] 的println(ID) 。}

並使用issue \ "project" \ "id"嵌套項目,然後創建新的Case類對象,並將其添加到列表var(可變,但我不知道如何以其他方式做)。但我收到編譯時錯誤:

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

我是新來斯卡拉和整體的Java基礎架構和框架,所以我會很感激的的代碼樣本。


PS。當我在我的案件類改變ID爲String現在我得到另一個錯誤:

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 

這是因爲「摘要」嵌套入「場」。所以,我的第二個問題仍然是實際的: 2.我如何處理嵌套值?

和新的相關問題: 3.如果我想使用Int爲id - 我該如何轉換它?

回答

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

在這種情況下,我繼續使用單例類(我定義我自己的需求結構),並解析嵌套的JSON性能進去,在飛行中做類型轉換需要的地方。

4

這是因爲json中的id字段以字符串形式出現(「id」:「10230」https://developer.atlassian.com/display/JIRADEV/JIRA+REST+API+Example+-+Query+issues),liftjson自動轉換爲JString。在您的案例類中,您需要將id字段設爲String。

2)用例類來處理嵌套的json。如果你有一個JSON看起來像這樣

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

,你將需要兩個case類

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

case class Summary(
    summary: String 
) 

3)我不認爲你可以將提取物中的方法來詮釋,因爲JSON結構定義它作爲一個字符串。在提取值後,轉換必須發生。

+1

感謝您的答覆 - 這是有幫助的。但是現在我收到一個錯誤:線程「main」中的異常net.liftweb.json.MappingException:摘要沒有可用的值 找不到可以轉換爲java.lang.String的值 - 這是因爲「summary」是嵌套的進入「田野」。 1.我如何處理嵌套值? 2.如果我需要使用Int作爲id - 我如何轉換它? –

+0

@KonstantinTrunin DId你在上面的評論中提到了你提到的問題的解決方案? TIA – texens