How to get names of objects contained in JSON with Java?

0

I needed to extract the name of the objects contained in a JSON file, for example, to show that the JSON file below has the objects batteryCharge, luminousFlux, id, temperature. But do this search without specifying the name of the object to be searched for, as I have other JSON files with different objects.

{
    "batteryCharge": {
        "metadata": {
            "code": {
                "type": "Text",
                "value": "%"
            }
        },
        "type": "urn:x-ogc:def:phenomenon:IDAS:1.0:batteryCharge",
        "value": "74"
    },
    "id": "urn:smartsantander:testbed:338",
    "luminousFlux": {
        "metadata": {
            "code": {
                "type": "Text",
                "value": "lm"
            }
        },
        "type": "urn:x-ogc:def:phenomenon:IDAS:1.0:luminousFlux",
        "value": "0"
    },
    "temperature": {
        "metadata": {
            "code": {
                "type": "Text",
                "value": "Cel"
            }
        },
        "type": "urn:x-ogc:def:phenomenon:IDAS:1.0:temperature",
        "value": "90"
    }
}

get this result:

batteryCharge, luminousFlux, id, temperature

Thanks in advance for your help.

    
asked by anonymous 29.03.2017 / 17:23

2 answers

0

A colleague helped me here ... I got the result I wanted with this function:

package buscaJson;

import java.io.*;
import org.json.*;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class BuscaAtributos {

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

        JSONParser parser = new JSONParser();

         try {

                Object obj = parser.parse(new FileReader("teste.json"));

                JSONObject jsonObject = (JSONObject) obj;
                System.out.println(jsonObject.keySet()); 

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            }
    }
}

Result obtained:

[batteryCharge, temperature, id, luminousFlux]
    
29.03.2017 / 19:50
0

You can use Gson :

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
    </dependency>

If you want to convert the entire json to object you will need to structure the classes:

code

public class Code {
    private String type;
    private String value;

   //gets e sets

}

metadata

public class Metadata {
    private Code code;

   //gets e sets

}

date

public class Data {
    private Metadata metadata;
    private String type;
    private String value;

   //gets e sets

}

and its object

public class MyOBJ {

    private String id;
    private Data batteryCharge;
    private Data luminousFlux;
    private Data temperature;

   //gets e sets

}

To use json's conversion to the object, just do this:

MyOBJ obj = new Gson().fromJson(json, MyOBJ.class);

Another way is to work directly with json, you can use org.json

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160810</version>
    </dependency>

and work with objects this way:

    JSONObject jsonObject = new JSONObject(json);

    JSONObject batteryCharge = jsonObject.getJSONObject("batteryCharge");
    JSONObject luminousFlux = jsonObject.getJSONObject("luminousFlux");
    String id = jsonObject.getString("id");
    JSONObject temperature = jsonObject.getJSONObject("temperature");
    
29.03.2017 / 17:59