简体   繁体   中英

How can i read and modify a json file in scala spark play

I want to read a JSON file and create a class/object that saved all label and value from every JSON's vector/record. Then I want to modify some values (or the JSON structure) and get this modified JSON file to Http request with play/spark/scala.

So how can I fill my class's variables with the json's values?

For example, I have this JSON file

   [
  {
    "ser": 345,
    "City": "New York",
    "Gen": 1
  },
  {
    "ser": 55,
    "City": "New York",
    "Gen": 2
  },
  {
    "ser": 19,
    "City": "New York",
    "Gen": 3
  }
 ]

My goal is create a class like this

class Book(ser:Integer, city:String, Gen:Integer)
{
   //TODO
}

That takes every value of ser, city and gen, from the json for all the records in the file. Than I want to modify the structure or values of the json, save and answer with the new file to a Http request.

Assuming you have this class

case class Book(ser: Int, city: String, gen: Int)

you can perform the JSON conversion using Play's JSON serialization/deserialization by implementing a formatter:

implicit val bookFormat: Format[Book] = {
  ((JsPath \ "ser").format[Int] and
    (JsPath \ "City").format[String] and
    (JsPath \ "Gen").format[Int]
    ) (Book.apply, unlift(Book.unapply))
}

// returns a sequence of Book objects
val books = Json.parse(bookJson).as[Seq[Book]]
// modify your books...
(...)
// convert back to Json
val json = Json.toJson(books)

由于您已经在使用Play,因此无需在项目中添加其他依赖项,就应该看看Play的json序列化/反序列化功能

Josef I'm writing here because is better.

Ok, so now I create the class that take the json (with format method). I have the seq, and i can make something like this:

books(9).city //return the city's value of the seq Book in 9 position of the seq

Ok so now I want to create a new Json with this structure:

[ { "label": [1,2,3] //that is the Gen value, 
     "values": [ 
                 { "label":"New York" 
                   "values":["200","10","66"]  //is the ser about all Gen (for example 200 is ser is for label/gen 1 .. 10 is for label/gen 2 etc.
                 },
                { "label":"London" 
                  "values":["500","150","46"] 
                }, 
                { "label":"London" 
                  "values":["500","150","46"] 
                }, 
                .
                . 
                . 
                . 
                . 
               ] 
  } 
]

That is different from the structure of the JSON input. I think that could be helpful create this new class:

class New_book(
label_gen:List[String], 
values_gen:List[Libri], 
label:String, 
values:List[Int]  
 )

Then i want to fill this new class with the book's seq ( that is this: "val books = Json.parse(JsonString).as[Seq[Libri]]" if you remember ).

After this, I will be querying the new Json with spark, and answer with it to an Http request.

I hope I was understood now :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM