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

Asked

Viewed 610 times

0

Needed to extract the name of objects contained in a JSON file, for example, show that the JSON file below has the batteryCharge objects, luminousFlux, id, temperature. But do this search without specifying the name of the object to be searched, 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

From now on, thank you very much for your help.

2 answers

0

You can use the Gson:

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

if you want to convert the whole 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 the json to Object conversion just do so:

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

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

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

and work with objects in 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");
  • Thanks for the suggestion!

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]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.