java - javax.json - How to check if a value is a string
<p>I have a database I made in Json. I have this parsed, and have the code iterating through a Json array, which contains several Json objects. Finally, I also am iterating through the values in the Json objects. My problem is that I need to check if the value that the iterator is on, is a string or an array. </p>

<p>Google seems to only have things like checking if there is a number in a string, or some other variation of that question.</p>

<p>SO questions doesn't appear to have much for what I want. There was a case that was very similar to mine, however it is dealing with primitive data types, so my case that is dealing with string doesn't work. (Here is the link to that other case: <a href="https://stackoverflow.com/questions/18097720/java-how-to-check-if-a-string-value-is-a-type-of-given-class">Java how to check if a string value is a type of given Class&lt;?&gt;</a> )</p>

<p>Here is a small portion of my database: The configuration arrays get longer as you go deeper into the database. I can not simply hold the values in a string, as "superscript" needs to be formatted as a superscript on the "term". I plan to do this later, but either way I will need to have the program figure out if it is dealing with a string or an array.</p>

<pre><code>{
"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"
        }
    ]
    }
}
</code></pre>

<p>Here is my code: The part of the code that is post to find out whether it is dealing with a string or a array is at the if statement (there is only 1 if statement)</p>

<pre><code>import javax.json.Json;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import javax.json.JsonObject;
import javax.json.JsonArray;
import javax.json.JsonValue;
import java.util.Iterator;
import java.util.Collection;
import java.io.FileReader;
import java.io.IOException;
import java.lang.String;

public class DataTest
{
public static void main(String[]args) throws IOException
{
    String strValue = "";
    JsonReader reader = Json.createReader(new FileReader("Elements.JSON"));
    JsonObject jsonst = reader.readObject();

    JsonArray elements = jsonst.getJsonArray("Elements"); 
    for (int i = 0; i &lt; elements.size(); i++) 
    {
        JsonObject element = elements.getJsonObject(i); 
        Collection&lt;String&gt; elementValues = element.keySet(); 
        Iterator&lt;String&gt; elementKeys = elementValues.iterator(); 
        while(elementKeys.hasNext())
        {
            if(elementKeys.next() instanceof java.lang.String)
            {
                System.out.println(elementKeys.next());
                String elementKey = elementKeys.next();
                strValue = element.getString(elementKey); 
                System.out.println(strValue); 
            }
        }
    }

    reader.close();
}
}
</code></pre>

<p>I think I have the if statement is almost completed, but I have into the original problem again. elementKeys.next() gets the name for a value. For example "name", "symbol", "nobleGasConfiguration". For the actual values (like "Hydrogen" from "name" : "Hydrogen"), I've been getting that as a string with the .getString() method. However, this causes the program to crash when it tries to get a array, because .getString() is for getting strings... Not arrays.</p>

<p>My main issue is getting the correct value to check. My current if statement always comes up as true. This is because the values that .next() gets are indeed strings, and therefore the instanceof returns true. This the case even for the array names. Then the program goes into the if statement and crashes when .getString() tries to get an array.</p>

<pre><code>if(elementKeys.next() instanceof java.lang.String)
</code></pre>

<p>So, my question. How do I safely get the right value (such as "Hydrogen" from "name" : "Hydrogen") for my if statement to check? (Doesn't matter if I need to use an entirely different method to check the value type, ultimately I need a means to check the value type)</p>

<p>What programs/versions am I using?</p>

<p>Notepad</p>

<p>Command-prompt</p>

<p>Java 8</p>

<p>javax.json-1.0.jar</p>

<p>Please keep the answers to java and javax.json.</p>