1

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Tyler
  • 957
  • 11
  • 27
  • I would highly recommend using an IDE instead of Notepad and the Command Prompt – OneCricketeer Mar 16 '16 at 00:55
  • I'm using command prompt and notepad because that's what we are doing in Computer Science 120. :P Once I get past this extra project I do plan to look at IDEs – Tyler Mar 16 '16 at 01:10

3 Answers3

1

try:

jsonst.getJsonArray("Elements")[0].getJsonString("name");

and also modify the line which says read instead of readObject:

JsonStructure jsonst = reader.readObject();
Nishanth Matha
  • 5,993
  • 2
  • 19
  • 28
  • I got 4 compiles errors. The first 3 lies in jsonst.getJsonArray("Elements").getJsonObject(0).getString("name"); Compile #1 ")" expected at the n in "("name"), Compile #2 unclosed string literal at the end (") of "("name")". Compile #3 "expected ";" at the end of the line". Compile #4, in my opinion the most usual, "reached end of file while parsing" – Tyler Mar 16 '16 at 01:05
  • Down to 1 error: cannot find symbol at the period in: jsonst.getJsonArray("Elements") This could be because of the specific library I downloaded could it not? – Tyler Mar 16 '16 at 01:32
  • know you solved your problem but refer to the updated ans it might be of help! – Nishanth Matha Mar 16 '16 at 03:23
1

So this line is okay to get the reader.

JsonReader reader = Json.createReader(new FileReader("Elements.JSON"));

Your problem is that JsonStructure is an interface that doesn't have the method getJsonArray, but JsonObject does.

Therefore, you need to be doing

JsonObject jsonst = reader.readObject();

And then you can use the methods of JsonObject like so

JsonArray elements = jsonst.getJsonArray("Elements");
for (int i = 0; i < elements.size(); i++) {
    JsonObject element = elements.getJsonObject(i);
    String name = element.getString("name");
}

Finally, when you are done with the reader, you need to close it.

reader.close();
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Finally, I got something that's working. Now I have a lot of experimenting to do, so I can fully understand this code. Thanks to all for your help! – Tyler Mar 16 '16 at 02:03
0
reader.readObject().getJsonArray("Elements").getJsonObject(0).getString("name")

Adding maven dependency details

    <dependency>
        <groupId>javax.json</groupId>
        <artifactId>javax.json-api</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.json</artifactId>
        <version>1.0.4</version>
    </dependency>
Sanj
  • 3,879
  • 2
  • 23
  • 26
  • I got "cannot find symbol" at the period in "jsonst.readArray()". I wonder if I am missing an import or two... – Tyler Mar 16 '16 at 01:13
  • Scratch that missing an import, doesn't make sense, since the read() worked, and read() and readArray() are with JsonReader – Tyler Mar 16 '16 at 01:17
  • I just noticed a major mistake on my part. So scratch those two comments above... – Tyler Mar 16 '16 at 01:19
  • Okay, now with that mistake fixed, i get a compile error saying that: String cannot be converted to JsonStructure – Tyler Mar 16 '16 at 01:22
  • updated the post with maven dependencies I am using. – Sanj Mar 16 '16 at 01:33
  • I don't believe I am using any dependecies... Let me see if I can find where I got this library – Tyler Mar 16 '16 at 01:35
  • @Sanj It was mentioned in the question that Notepad is being used. I highly doubt this is a Maven project. – OneCricketeer Mar 16 '16 at 01:37
  • Yea, I've seen Maven when looking for a library, but I shied away from dependecies, and I believe this library I am using has no dependecies. However, I am not very educated in this particularly subject. I also just checked the jar file, and there something about maven in it. javax.json-1.0.jar > META-INF > maven etc. – Tyler Mar 16 '16 at 01:44