I have this sample JSON object
{
"Elements" : [
{
"name" : "Hydrogen",
"Symbol" : "H",
"atomicNumber" : "1",
"electronegativity" : "2.2",
"group" : "Hydrogen",
"ionCharge1" : "1+",
"ionCharge2" : "1-",
"molarMass" : "1.01",
"naturalState" : "Gas",
"synthetic" : "false",
"diatomic" : "true",
"columnNumber" : "1",
"row" : "1",
"columnCode" : "IA",
"nobleGasConfiguration" : [
{
"term:" : "No Noble Gas Configuration",
"superScript" : "-"
}
],
"electronConfiguration" : [
{
"term" : "1s",
"superScript" : "1"
}
]
}
}
Through the following code I've gotten the Json database into a JsonStructure.
import javax.json.Json;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import java.io.*;
public class DataTest
{
public static void main(String[]args) throws IOException
{
String strName;
JsonReader reader = Json.createReader(new FileReader("Elements.JSON"));
JsonStructure jsonst = reader.read();
/*strName = jsonst.
* get.JsonObject(String name)
* get.JsonArray(String name)
* get.JsonString(String name).getString()
*/
}
}
What I want to do is simply get the value of "name", that value being "Hydrogen", and this value would be placed in the variable strName.
I've been trying to get basic things like this done for a couple of days, and anything that got somewhere I was derailing myself from my real intentions. Everything else just failed or never worked.
My latest attempt was using the methods commented out at the bottom of the code, I believe I have the methods I need to get this done. (These methods were obtained from the following link: https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html )
What I had tried to do was:
jsonst.getJsonArray("Elements").getJsonObject(0).getJsonString("name").getString();
This gave me a "cannot find symbol" compile error with a ^ at the period in "jsonst.getJsonArray("Elements")"
So, what am I doing wrong? How can I get this simple task done?
What programs, text-editors, versions, etc am I using?
- Command prompt
- Notepad
- Java 8
- javax.json-1.0.jar
Please keep the answers to java and javax.json, one of the tough roads I've slowly ventured was getting a library to use json. I'd rather not go through the trouble of finding another.