0

JSON string used:

'{"Sensors":[{\"name\":\"BLRB50CM_A\",\"cameraId\":\"Cam10\",\"id\":1,\"resolution\":\"1280 x 720\",\"officeLocation\":\"Offshore Development Center\",\"tags\":\"Entrance Camera, Parking Lot\",\"isActive\":\"true\",\"hls\":\"https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8\",\"rtsp\":\"rtsp://10.66.102.66:32278/mystream/parking_lot\",\"type\":\"sensor\",\"inputs\":[],\"outputs\":[2]}]}'

java pojo class:

public class ServiceFlowData {

    public ArrayList<Sensor> sensors;
    
    public ArrayList<Sensor> getSensors() {
        return sensors;
    }

    public void setSensors(ArrayList<Sensor> sensors) {
        this.sensors = sensors;
    }
} 

code used for conversion to java object

Gson gson = new Gson();
ServiceFlowData serviceFlowData = gson.fromJson(jsonString,ServiceFlowData.class);
System.out.println("serviceFlowData"+serviceFlowData.getSensors());

iam getting get sensors as null.

what is it iam mising here..?

avaj
  • 95
  • 1
  • 1
  • 6
  • You can use this link : https://stackoverflow.com/questions/55533093/convert-json-string-to-json-objects/55533405#55533405 – Pawan Tiwari Nov 03 '21 at 04:38
  • Class must have the same variables as in the JSON that you want to map with it, and they are case-sensitive! – dhakalkumar Nov 03 '21 at 05:16

1 Answers1

0

Your json key is Sensors and not sensors.

Solution 1

Try changing

public ArrayList<Sensor> sensors;

to

public ArrayList<Sensor> Sensors;

Solution 2

You can use @SerializedName annotation to map a json key to a java variable with a different name.

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78