Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

spring - Java get one JSONObject from a JsonArray and set json response

I have a Json array

{
   "id":33,
   "dataContent":"user1",
   "temp":"36"
},
{
   "id":33,
   "dataContent":"user2",
   "temp":"36"
},
{
   "id":33,
   "dataContent":"user3",
   "temp":"39"
},
{
   "id":21,
   "dataContent":"user4",
   "temp":"38"
},
{
   "id":22,
   "dataContent":"user5",
   "temp":"37"
}

that am generating using the code

@PostMapping(value = "/temperature")
    public ResponseEntity<?> Temperature(@Valid @RequestBody Activation activation) {
        Temperature temperature = new Temperature();
        temperature.setCode(activation.getDataContent());
        temperature.setDatecreated(new Date());
        temperature.setUser_id(activation.getId());
        temperature.setTemperature(activation.getTemp());
        temperatureRepository.save(temperature);
        
        return ResponseEntity.ok("Temperature Updated");
    }

how can I set a check in such a way that the if the temperature is greater than 38 set a json response as not cleared and if it is less than 38 the response returned is cleared.

{

   "message":"cleared"

}
question from:https://stackoverflow.com/questions/65540642/java-get-one-jsonobject-from-a-jsonarray-and-set-json-response

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Create a class with field named message.

     public class Message {
    
         private String message;
    
         public String getMessage() {
             return message;
         }
    
         public void setMessage(String message) {
             this.message = message;
         }
    
    
     }
    
  2. Then, in your above method,

     Message m = new Message();
    
     if(activation.getTemp() > 38) {
         m.setMessage("not cleared);
         return ResponseEntity.ok().body(m);
    
     }
     else{
         m.setMessage("cleared");
         return ResponseEntity.status.ok().body(m);
     }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...