Thursday, January 12, 2017

JSON in Java

Add to your Maven dependency
1
2
3
4
5
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.0.4</version>
</dependency>
Demo:
1
2
3
4
5
6
7
String json = "{\"id\":3}";
JsonReader reader = Json.createReader(new StringReader(json));
JsonObject rootObject = reader.readObject();
reader.close();

int devID = rootObject.getInt("id");
String sDevID = Integer.toString(devID);

No comments: