Java JSON POJO mapping

280 views Asked by At

I am using the met office for weather data for which they have 171 weather sites in the UK. This is going into a mongo database. As the weather sites differ in sensors and capability so then does the data you get back from it. An example of two sites is shown below.

My question is how do you POJO such a JSON structure if data can change as it does.What is a best approach for such an instance.

 {
   "_id": ObjectId("54ca68c8ccf2562b665edb53"),
   "dataDate": "2015-01-29T16:00:00Z",
   "i": "3609",
   "lat": "51.565",
   "lon": "-3.981",
   "name": "MUMBLES HEAD",
   "country": "WALES",
   "continent": "EUROPE",
   "elevation": "32.0",
   "Period": {
     "type": "Day",
     "value": "2015-01-29Z",
     "Rep": {
       "D": "W",
       "G": "37",
       "H": "73.8",
       "P": "996",
       "S": "13",
       "T": "4.9",
       "Pt": "F",
       "Dp": "0.6",
       "MinSinceMidnight": "240"
    }
  }
} 

Unfortunately there are other sites with less sensors etc so the data can look like this:

 {
   "_id": ObjectId("5576c770e4b046add9b0126f"),
   "dataDate": "2015-06-09T10:00:00Z",
   "i": "99159",
   "lat": "50.61359",
   "lon": "-1.95875",
   "name": "SWANAGE",
   "country": "ENGLAND",
   "continent": "EUROPE",
   "elevation": "10.0",
   "Period": {
     "type": "Day",
     "value": "2015-06-09Z",
     "Rep": {
       "H": "59.7",
       "T": "13.6",
       "Dp": "5.9",
       "MinSinceMidnight": "600"
    }
  }
} 

1

There are 1 answers

0
forzagreen On BEST ANSWER

Use the JSONObject class from the org.json java library.