[英]Java - javax.json - Compile Error: “cannot find symbol” for .getValueType() - Can't seem to figure out why
我有一个数据库,并通过代码将数据库放入JsonArray,并开始通过JsonArray进行迭代(仅查找JsonObjects)。 从那里,我相信我已经有了此设置,因此可以遍历每个JsonObject的属性。 但是,每个JsonObject都包含Json字符串和Json数组。 现在,我正在尝试,所以我尝试为数组创建一个手动异常,基本上,我试图设置它的方式是,只有在处理Json String时才做您的工作。
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;
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 < elements.size(); i++)
{
JsonObject element = elements.getJsonObject(i);
Collection<String> elementValues = element.keySet();
Iterator<String> elementKeys = elementValues.iterator();
while(elementKeys.hasNext())
{
//Error is over here!!!
if(elementKeys.next().getValueType().equals(JsonValue.ValueType.STRING))
{
String elementKey = (String)elementKeys.next();
strValue = element.getString(elementKey);
System.out.println(strValue);
}
}
}
reader.close();
}
}
我相信我的导入正确,但是当我尝试对此进行编译时,编译器在.getValueType()方法中为我提供“找不到符号”。 (我在代码中留下了一条注释,指出“错误在这里!!!”,此方法所在的行在该注释的正下方)
我还检查了比较的格式,以下内容是从以下位置复制并粘贴的: http : //www.programcreek.com/java-api-examples/index.php? api= javax.json.JsonString if((results .getValueType()。equals(JsonValue.ValueType.STRING))
这是我数据库的一小部分:
{
"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"
}
]
}
}
我怀疑这可能与诸如子接口,超级接口,实现的类等有关。本质上,尝试使用无法访问该方法的方法。 但是,据我了解,这应该有效。
这是您可能需要的api文档:
http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html
http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javaee/7/api/javax/json/JsonValue.html
https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
https://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html
为了清楚地说明我的问题...编译器为什么给我一个“找不到符号”? 我必须在代码中进行哪些更改以解决此问题?
我正在使用什么程序,版本等?
记事本
命令提示符
Java 8
javax.json-1.0.jar
请保留对javax.json和java的答案。
您收到错误消息是因为elementKeys.next()
返回一个String
,它没有getValueType()
方法。 您不需要if检查,也不需要(String)elementKeys.next();
。 只需删除这些,您就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.