[英]Parsing DBpedia JSON from localhost extraction server in java when the schema isn't known
This is probably a quick answer to a very novice question. 这可能是一个非常新手的问题的快速答案。 I am having trouble wrapping my head around how to get JSON text dbpedia extraction server running from a localhost.
我无法解决如何从本地主机运行JSON文本dbpedia提取服务器的问题。 The server is running fine, I followed the official instructions .
服务器运行正常,我按照官方说明进行操作 。
I have read the other StackOverflow questions about parsing JSON in java and what I am having trouble understanding is how to parse the JSON when the schema or structure is unknown. 我已经阅读了有关在Java中解析JSON的其他StackOverflow问题,我无法理解的是在架构或结构未知时如何解析JSON。
For example in my code I try to grab the JSON from localhost and put it into a java object. 例如,在我的代码中,我尝试从本地主机获取JSON并将其放入Java对象。 But all the examples of parsing JSON online use a predesigned java object and all the JSON keys are mapped to an object's fields.
但是,所有在线解析JSON的示例都使用预先设计的java对象,并且所有JSON密钥都映射到对象的字段。 (ie Employee class: name,job,email,id,phone)
(即员工类别:姓名,职位,电子邮件,ID,电话)
String sURL = "http://localhost:9999/server/extraction/en/extract?title=" + wikipage + "&revid=&format=rdf-json&extractors=custom"; //just a string
URL url = new URL(sURL);
Reader pageReader = new InputStreamReader(url.openConnection().getInputStream());
Gson g = new Gson();
JsonReader jr = new JsonReader(new InputStreamReader((InputStream) request.getContent()));
jr.setLenient(true);
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //convert the input stream to a json element
JsonObject rootobj = root.getAsJsonObject(); //may be an array, may be an object.
I now have this "json object" for the film "Blue Velvet" I can parse/iterate with jr.hasNext() or rootobj.getAsJsonArray(). 现在,我可以使用jr.hasNext()或rootobj.getAsJsonArray()解析/迭代电影“ Blue Velvet”的“ json对象”。 Am I going about this correctly?
我能正确处理吗?
I feel like I am reinventing the wheel. 我觉得自己正在重新发明轮子。 Is there a standard way of parsing DBpedia JSON objects in Java?
有没有Java中解析DBpedia JSON对象的标准方法?
At least the Jackson JSON library allows you to parse incoming JSON into a Map. 至少Jackson JSON库允许您将传入的JSON解析为Map。 If the keys and values of the JSON can be of any type, then you need to use
Map<Object, Object>
, which is a bit cumbersome, but anyways this should work: 如果JSON的键和值可以是任何类型,则需要使用
Map<Object, Object>
,这有点麻烦,但是无论如何这应该起作用:
ObjectMapper mapper = new ObjectMapper();
Map<Object, Object> parsedJSON = mapper.readValue(incomingJSON,
mapper.getTypeFactory().constructMapType(
LinkedHashMap.class,
Object.class,
Object.class));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.