0

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"
    }
  }
}	

2
  • I think you have many ways of achieve this task. As I can see the data only different in Rep object. So for example you could model your class which contains all other fields and specify that Rep field is a map for example. Another approach could be to specify class with all possible values and use annotation to make them optional. Commented Jun 9, 2015 at 11:29
  • One possible way would be to create exhaustive list of all the possible fields, and implement a class that contains them all. Then, simply initialize absent values with nulls. Commented Jun 9, 2015 at 11:34

1 Answer 1

1

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

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.