简体   繁体   English

如何从URL读取JSON数据到XPage Java Bean

[英]How to read JSON data from URL into XPage Java Bean

I get that typically this is something better done with Client Side JavaScript. 我知道这通常是使用Client Side JavaScript更好的方法。 However CSJS is not ideal in this situation for other reasons. 然而,出于其他原因,CSJS在这种情况下并不理想。

BUT if I have a Java object inside XPages. 但是如果我在XPage中有一个Java对象。 A managed bean maybe or whatever. 托管bean可能或其他什么。 Is it possible to read in JSON information to the object for parsing purposes? 是否可以将JSON信息读入对象以进行解析? In theory I'd want to process the data and use it in a TypeAhead for an editbox control. 从理论上讲,我想要处理数据并在TypeAhead中使用它来进行编辑框控件。

The reason I'm interested in this rather then using the more tradition domino object model is the speed of getting the entries. 我对此感兴趣而不是使用更传统的多米诺骨牌对象模型的原因是获得条目的速度。 I'm wondering if a view thats using the great "startkey" parameter is faster then something like getAllEntriesByKey(). 我想知道使用伟大的“startkey”参数的视图是否比getAllEntriesByKey()更快。

For instance this view: http://xpagescheatsheet.com/fakenames.nsf/1fcbbeb633d9fdb3852576480057a7dc?ReadViewEntries&outputformat=JSON&StartKey=Fav 例如这个视图: http//xpagescheatsheet.com/fakenames.nsf/1fcbbeb633d9fdb3852576480057a7dc?ReadViewEntries&outputformat=JSON&StartKey=Fav

To to sum up I'm look to see if it's possible to injest the data from that link into a Java Object from XPages. 总结一下,我想看看是否有可能将来自该链接的数据从XPages中压入Java对象。 the key could be anything... doesn't have to be "Fav". 关键可以是任何东西......不一定是“收藏”。

Thank you! 谢谢!

There are 2 parts to the question: 该问题分为两部分:

  1. How to read data from an URL. 如何从URL读取数据。 Here Oliver provided the hint: use the Apache HTTP client (the libraries are in XPages) 在这里,Oliver提供了提示:使用Apache HTTP客户端(库是XPages中的)
  2. How to turn that JSON into a Java object. 如何将JSON转换为Java对象。 Here I would use Google GSON and a matching object. 在这里,我将使用谷歌GSON和匹配的对象。 The Apache HTTP client gives you access to an input stream for the body. Apache HTTP客户端允许您访问正文的输入流。 Something like 就像是

    SomeClass createFromJson(InputStream in) { Gson gson = new GsonBuilder().create(); SomeClass sc = gson.fromJson(in, SomeClass.class); return sc; }

I definitely support the way Stefan suggests - and I do that myself. 我绝对支持Stefan建议的方式 - 而且我自己这样做。

If you wrap Gson in a plugin you need to take care of security and permissions. 如果您将Gson包装在插件中,则需要注意安全性和权限。 I have just recently written an article about exactly this (with Gson as the example) 我刚刚写了一篇关于这个的文章 (以Gson为例)

However, if you do not want to add extra libraries to your application then you can also use the built-in JSON functionality (in package: com.ibm.commons.util.io.json) to obtain the same - though not in the same elegant way. 但是,如果您不想在应用程序中添加额外的库,那么您还可以使用内置的JSON功能(在包中:com.ibm.commons.util.io.json)来获取相同的内容 - 尽管不在同样优雅的方式。

Here is a small example: 这是一个小例子:

JsonJavaFactory factory = JsonJavaFactory.instanceEx;
Object jsonObjs;
try {
    String url = "http://.....";
    jsonObjs = JsonParser.fromJson(factory, new BufferedReader(new InputStreamReader(new URL(url).openStream())));
    for (Iterator<Object> val = factory.iterateArrayValues(jsonObjs); val.hasNext();) {
        JsonJavaObject obj = (JsonJavaObject) val.next();
        Double idNum = (Double) obj.getJsonProperty("id");
        String id = Integer.toString(idNum.intValue());
        if (StringUtil.isNotEmpty(id)) {
            String name = (String) obj.getJsonProperty("name");
            // Do something with id and name...
        }
    }
} catch (JsonException e) {
    e.printStackTrace();
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();

/John /约翰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM