Memphis Meng
Memphis Meng

Reputation: 1681

Iterate Json object using Scala2

Newbie Scala user's here! I was trying to extract all elements in a JSON array one by one, so I was iterating this object. Since Scala doesn't have built-in JSON support, I used a third-party JSON parsing package ujson. I am trying to implement a for-loop on a JSON array like this:

var json = ujson.read("""{"list": ["id":1, "id":2, "id":3]}""")
var index = 0
var iteration: Boolean = true
while(iteration) {
  try {
    print(json("list")(index))
    index += 1
  }
  catch {
    iteration = false
  }
}

It encountered a compilation error on the line iteration = false and said type mismatch;. So in this question, I am looking for the things listed below:

  1. Are there any 3rd-party packages able to handle the iteration on JSON arrays? I've only tried 3 of them I knew so I can't guarantee we can't iterate without a dummy function like mine.
  2. If I keep using upickle, is it the way we do iteration?
  3. What does the error stand for in my code?

Scala version: 2.13.12

Upvotes: 0

Views: 100

Answers (0)

Related Questions