简体   繁体   中英

Get specific values by giving specific id - JSON file using JAVA

I would like to read and print the values of a specific id . For example, I would like to read and print the name and the status of the sensor with id = 1 . How can I do this using JAVA and JSON file? Could anybody help me?

{
    "Sensor": [
          {
           "id": 1,
           "name": "RR",
           "status": 1,
           },
          {
           "id": 2,
           "name": "RS",
           "status": 1,
           },
          {
           "id": 3,
           "name": "GR",
           "status": 0,
          },

        ],
}

JAVA code to read JSON file :

public class JSON {

    private static String jsonFile = "/Users/foteini/Desktop/JSON/sensor copy.json";   

    public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {

       FileReader reader = new FileReader(jsonFile);

       JSONObject jsonObject = (JSONObject) new JSONParser().parse(reader);

       JSONArray sensors = (JSONArray) jsonObject.get("Sensor");
       //take the elements of the json array
       int id_num=0;
       for (int i=0; i<sensors.size(); i++){
           System.out.println("The sensors in the array:" + sensors.get(i) + "\n");

       }
   }
  }

Thanks a lot for your help!

Your sensor is a JSONArray not a JSONObject so

1.) Fetch your array

2.) Traverse array using indexes and fetch object from array

3.) Fetch values from objects

   JSONObject jsonObject = (JSONObject) new JSONParser().parse(reader);

   // fetch sensor array
   JSONArray sensors = jsonObject.getJSONArray("Sensor");
   JSONObject temp;
   //take the elements of the json array
   int  id ;
   String name,status;

   for (int i=0; i<sensors.size(); i++){
       // fetch array using index
       temp = sensors.getJSONObject(i); 

       // fetch your data
       id = temp.optInt("id");

       if( id==1){
           name   = temp.optString("name");
           status = temp.optString("status");
           System.out.println( name +" "+status);
       }
   }
public class JSON {

/**
 * @param args the command line arguments
 */

private static String jsonFile = "/Users/foteini/Desktop/JSON/sensor copy.json";   

public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {

    FileReader reader = new FileReader(jsonFile);

    JSONObject jsonObject = (JSONObject) new JSONParser().parse(reader);

    JSONArray sensors = (JSONArray) jsonObject.get("Sensor");
    //take the elements of the json array
    int id_num=0;
    for (int i=0; i<sensors.size(); i++){
        JSONObject item = sensors.get(i);
        if (item.getInt("id") == 1){
            // process the item
            System.out.println(item.getString("status"));
        }

     }
   }
}

You have to specify condition int the loop:

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