0

With reference to answer in following post: Find the maximum value from JSON data in Scala

I am very new to programming in Scala, and as one solution states in the mentioned post, I am testing following code:

import collection.immutable.IndexedSeq
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser

case class wrapperObject(val json_string: Array[MyJsonObject])
case class MyJsonObject(val id:Int ,val price:Int)

object Demo {

    val gson = new Gson()
    def main(args: Array[String])={
    val json_string = scala.io.Source.fromFile("jsonData.txt").getLines.mkString
    //val json_string= """{"json_string":[{"id":1,"price":4629},{"id":2,"price":7126},{"id":3,"price":8862},{"id":4,"price":8999},{"id":5,"price":1095}]}"""
    val jsonStringAsObject= new JsonParser().parse(json_string).getAsJsonObject
    val objectThatYouCanPlayWith:wrapperObject = gson.fromJson(jsonStringAsObject, classOf[wrapperObject])
    var maxPrice:Int = 0
    for(i <- objectThatYouCanPlayWith.json_string if i.price>maxPrice) 
    {
        maxPrice=  i.price
    }
    println(maxPrice)
}
}

I am getting following error at line 15. java.lang.IllegalStateException: Not a JSON Object:

The contents of the JSON file are as follows:

[{ "id":978,"price":2513},
{ "id":979,"price":8942},
{ "id":980,"price":1268},
{ "id":981,"price":5452},
{ "id":982,"price":5585},
{ "id":983,"price":9542}]

Not sure as to why this error is appearing. Any help would be appreciated. Thanks.

3
  • Your file does not contain a valid json. Please share the contents of jsonData.txt, but I would just search online for a json formatter that will highlight your issue for you. Commented Dec 7, 2016 at 17:53
  • Edited the question with contents of the JSON file. I have searched and tried using GSON. Commented Dec 7, 2016 at 17:58
  • I think your code may have worked if you had used .getAsJsonArray instead of .getAsJsonObject Commented Dec 7, 2016 at 18:41

1 Answer 1

1

Your JSON file is not valid JSON format. As per the logic you implemented with wrapperObject, your file should look like this:

{
    "json_string": [
        { "id":978,"price":2513},
        { "id":979,"price":8942},
        { "id":980,"price":1268},
        { "id":981,"price":5452},
        { "id":982,"price":5585},
        { "id":983,"price":9542}
    ]
}

which will then give the output 9542. Note that your commented out version of json_string is actually valid and the output would be 8999.

JSON format is an attribute-value pair, but your file only has value - which is Array[MyJsonObject]. As per your wrapperObject case class, json_string is the attribute, and it's needed for Gson to parse the data into an object of type wrapperObject.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.